这个简单的 if else c++ 代码有什么问题?
what is wrong in this easy if else c++ code?
这个问题在 hackerrank 条件语句中,我想在不使用 array.The 代码运行但给出错误输出的情况下解决它。谁能看出这段代码有什么错误,为什么它不起作用?
#include <bits/stdc++.h>
using namespace std;
int n ;
cin >> n ;
int main()
{
int n ;
cin>>n;
if(n=0){
cout<<"zero";
}
else if(n=1){
cout<<"one";
}
else if(n=2){
cout<<"two";
}
else if(n=3){
cout<<"three";
}
else if(n=4){
cout<<"four";
}
else if(n=5){
cout<<"five";
}
else if(n=6){
cout<<"six";
}
else if(n=7){
cout<<"seven";
}
else if(n=8){
cout<<"eight";
}
else if(n=9){
cout<<"nine";
}
else{
cout<<"Greater than 9";
}
return 0;
}
您应该使用“==”而不是“=”
因为“if (n=1)”的行为与“if(true)”相同
== 比较
= 是作业
这个问题在 hackerrank 条件语句中,我想在不使用 array.The 代码运行但给出错误输出的情况下解决它。谁能看出这段代码有什么错误,为什么它不起作用?
#include <bits/stdc++.h>
using namespace std;
int n ;
cin >> n ;
int main()
{
int n ;
cin>>n;
if(n=0){
cout<<"zero";
}
else if(n=1){
cout<<"one";
}
else if(n=2){
cout<<"two";
}
else if(n=3){
cout<<"three";
}
else if(n=4){
cout<<"four";
}
else if(n=5){
cout<<"five";
}
else if(n=6){
cout<<"six";
}
else if(n=7){
cout<<"seven";
}
else if(n=8){
cout<<"eight";
}
else if(n=9){
cout<<"nine";
}
else{
cout<<"Greater than 9";
}
return 0;
}
您应该使用“==”而不是“=” 因为“if (n=1)”的行为与“if(true)”相同
== 比较 = 是作业