新手程序员在这里为我的计算机科学同事学习 我的当前任务有问题,而且我正试图弄清楚我的2个语法错误在哪里。我一直在寻找我已经完成的类似任务,但无济于事我无法找到我遇到的问题。
我在这两行上遇到以下错误:
'其他'没有先前的'如果' @"否则if(num> = 1&& num< = 50&& num%2 == 0)//检查1到50之间的偶数#"
在'%'之前预期的主要表达令牌@"否则if(num> 50 %% num< = 100&& num%10 == 0)//检查51和100之间的MULTIPLE OF 10"
using namespace std;
int main ()
{
cout << "Jon Grezda CS 12 Wednesday 6-8pm" << endl;
cout << "Assignment #9" << endl;
int num;
//num - Number input
cout << "Please enter a whole number between the numbers 1-100." << endl;
cin >> num; //Is the number in the right range?
if (num < 1 || num > 100) //INVALID Number, Terminate program
cout << "INVALID NUMBER, rerun program and please try again" << endl;
cout << "Program Terminated";
else if (num >= 1 && num <= 50 && num % 2 == 0) //check for EVEN number between 1 and 50
cout << "The number " << num << " is an EVEN number";
else if (num >= 1 && num <= 50 && num % 2 == 1) //check for ODD number between 1 and 50
cout << "The number " << num << " is an ODD number";
else if (num > 50 % % num <= 100 && num % 10 == 0) // Check for MULTIPLE OF 10 between 51 and 100
cout << "The number " << num << "is A MULTIPLE OF 10";
else // Check for NOT A MULTIPLE OF 10 between 51 and 100
cout << "The number " << num << "is NOT A MULTIPLE OF 10";
}
答案 0 :(得分:3)
if
和else
之间有两条陈述。您需要使用大括号{ }
将这些语句分组为一个。
if (num < 1 || num > 100) //INVALID Number, Terminate program
{
cout<<"INVALID NUMBER, rerun program and please try again"<<endl;
cout<<"Program Terminated";
}
else if (num >= 1 && num <= 50 && num % 2 == 0) //check for EVEN number between 1 and 50
答案 1 :(得分:1)
添加分组为@ 1201ProgramAlarm建议然后 在以下条件中将“%%”替换为“&amp;&amp;”
if (num > 50 %% num <= 100 && num % 10 == 0) // Check for MULTIPLE OF 10 between 51 and 100
完整代码:
using namespace std;
int main ()
{
cout << "Jon Grezda CS 12 Wednesday 6-8pm" << endl;
cout << "Assignment #9" << endl;
int num;
//num - Number input
cout << "Please enter a whole number between the numbers 1-100." << endl;
cin >> num; //Is the number in the right range?
if (num < 1 || num > 100) //INVALID Number, Terminate program
{
cout << "INVALID NUMBER, rerun program and please try again" << endl;
cout << "Program Terminated";
}
else if (num >= 1 && num <= 50 && num % 2 == 0) //check for EVEN number between 1 and 50
{
cout << "The number " << num << " is an EVEN number";
}
else if (num >= 1 && num <= 50 && num % 2 == 1) //check for ODD number between 1 and 50
{
cout << "The number " << num << " is an ODD number";
}
else if ((num > 50 && num <= 100) && num % 10 == 0) // Check for MULTIPLE OF 10 between 51 and 100
{
cout << "The number " << num << "is A MULTIPLE OF 10";
}
else // Check for NOT A MULTIPLE OF 10 between 51 and 100
{
cout << "The number " << num << "is NOT A MULTIPLE OF 10";
}
}