如果CIN真或变量C++假
问题描述:
请帮我解决这个..如果CIN真或变量C++假
int A=10;
cout<<"Answer this Question!";
cout<<"5 + 5 = ";cin>>A;
if(true) {
cout<<"Correct!"<<endl;
} else if (false) {
cout<<"Wrong!"<<endl;
}
getch();
}
答案是A = 10,如果用户输入正确的从int A=10
回答则通知符使用COUT“正确!”如果错误是“错误!” 。
答
在你上面的代码,
如果(真)
结果始终为true,因此会一直打印“正确!”。 但是,我猜你在问我们如何检查用户的输入以查看它们是否正确?在这种情况下,你的if语句,你想
if (A == 10)
而非一个else如果只是因为比其他10个是不正确的答案的任何一个else语句。
我建议你查阅编程的基础知识。
答
使用这样的事情:)
//...
int B;
cin>>B;
if(A == B) {
cout<<"Correct!"<<endl;
} else {
cout<<"Wrong!"<<endl;
}
答
int main(){
int A = 0;
std::cout<<"Answer this question: 5 + 5 = ";
std::cin>>A;
if(A == 10){
std::cout<<"Correct"<<std::endl;
}else{
std::cout<<"Wrong"<<std::endl;
}
return 0;
}
答
可以使用两个独立的变量,一个是用户的回答,一个是正确答案,然后对它们进行比较:
int correctAnswer = 10;
int userAnswer = 0;
cout << "Answer this Question!";
cout << "5 + 5 = ";
cin >> userAnswer;
if (correctAnswer == userAnswer) {
cout << "Correct!" << endl;
} else {
cout << "Wrong!" << endl;
}
getch();