我正在为一个开始的C ++课程做家庭作业,我有点迷失 这是作业:
创建一个c ++程序,要求用户输入一个数字。
程序的输出应为以下之一: 您输入了偶数。 要么 您输入了一个ODD号码。
如果用户输入了ODD号码,请他们输入另一个号码。 将此数字乘以第一个数字并输出结果。
偶数/奇数部分非常简单 - 我得到了那部分工作。我在第二部分完全失败了。我遇到了很多错误,我甚至无法弄清楚它的起点。如果有人能给我一个关于我做错的提示,我会非常感激。
#include <iostream>
using namespace std;
int main () {
int num1; // This is the original number entered by the user.
int num2; // This is the second number entered if the first number is odd.
cout << "Enter a number: "<< endl;
cin >> num1 >> endl;
if (num1 % 2 == 0) {
cout << num << " Your number is even." << endl;
} if (num1 % 2 != 0) {
cout << num1 << " Your number is odd. Please enter another number: “<< endl;
cin >> num1 >> endl;
} // end of if odd
cout << " Your two numbers multiplied equals (num1 *= num2)” << endl;
} // end of main ()
答案 0 :(得分:2)
#include <iostream>
using namespace std;
int main () {
int num1; // This is the original number entered by the user.
int num2; // This is the second number entered if the first number is odd.
cout << "Enter a number: "<< endl;
cin >> num1;
if (num1 % 2 == 0) {
cout << num1 << " Your number is even." << endl;
}
else {
cout << num1 << " Your number is odd. Please enter another number: " << endl;
cin >> num2;
cout << " Your two numbers multiplied equals " << num1*num2 << endl;
} // end of if odd
return 0;
} // end of main ()
这是固定代码。您尝试cout << num
,但没有num
变量,应该是num1
,cin >> endl
也是错误的。
出乎意料的是,您最后的”
不是"
,而是其他内容,并且会产生很多错误。
答案 1 :(得分:0)
奇数值的部分
if (num1 % 2 != 0) {
cout << num1 << " Your number is odd. Please enter another number: “<< endl;
cin >> num2 >> endl;
cout << " Your two numbers multiplied equals:" << (num1 * num2) << endl;
}
答案 2 :(得分:0)
if (num1 % 2 != 0) {
cout << num1 << " Your number is odd. Please enter another number:"<< endl;
cin >> num2;
cout << " Your two numbers multiplied equals:" << (num1 * num2) << endl;
}
不要在配额标记之间放置公式。这会把它们变成不能按要求执行的字符串或字符。即cout << " Your two numbers multiplied equals (num1 *= num2)” << endl;
将语句cout << " Your two numbers multiplied equals (num1 *= num2)” << endl;
放在if语句旁边会导致语句运行,即使该数字不是奇数。这不符合任务。并且num2为null仍会导致错误