我试图用 C++ 编写一个程序,它应该订购 3 个随机数(用户将编写它们),然后打印较高和较低的数字,但如果两个或更多数字相等,则应打印它。
新代码:
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
cout << "num1" << endl;
cin >> num1;
cout << "num2" << endl;
cin >> num2;
cout << "num3" << endl;
cin >> num3;
if(num1 == num2 && num3==num2 && num1==num3){
cout << "all numbers are equal";
}
else if (num1 == num2){
cout << "num1 and num2 are equal";
}
else if (num2 == num3){
cout << "num2 and num3 are equal";
}
else if(num3 == num1){
cout << "num1 and num3 are equal";
}
else{
if (num1 != num2 && num2 != num3 && num3 != num1){
if (num1 > num2 && num1 > num3){
cout << "higher is num1";
}
else if(num2 > num1 && num2 > num3){
cout << "higher is num2";
}
else if(num3 > num1 && num3 > num2){
cout << "higher is num3";
}
}
}
return 0;
}
新问题: 程序也需要知道哪个是最低的,那我该怎么做呢?
旧代码:
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
cout << "num1" << endl;
cin >> num1;
cout << "num2" << endl;
cin >> num2;
cout << "num3" << endl;
cin >> num3;
if(num1 == num2 && num3==num2 && num1==num3){
cout << "all your numbers are equal";
}
if (num1 != num2 && num2 != num3 && num3 != num1){
if (num1 > num2 && num1 > num3){
cout << "num1";
}
else if(num2 > num1 && num2 > num3){
cout << "num2";
}
else /*(num3 > num1 && num3 > num2)*/{//Here I tried to use and else if
cout << "num3";
}
}
return 0;
}
老问题 这段代码都是错的,但不知道自己做错了什么,请帮帮我。我有最后一个问题,我对 if 语句的 if 有限制吗?或者我不能写另外两个 if or..?谢谢。