如何获得两个数相等或这个数更大的值? 在C++中,我正在经历一个在线课程,我不知道从哪里可以找到这个或好的C++网站的答案。 下面的代码应该给出输出:两个数字等于或大于a/b #include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { int a,b,c=0; cout<<"Enter value of 1st variable: "; cin>>a; cout<<"Enter value of 2nd variable: "; cin>>b; { if ( !(a == b)); !true; cout << "Both are equal"; } !false; { if (a>b) c = a; if (b>a); c = b; cout << "Greater value is " << c; } } #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { int a,b,c=0; 库塔; 库特布; { 如果(!(a==b)); 符合事实的 法院(b) c=a; 如果(b>a); c=b; CUT显然,如果C++中的其他方法正确,你就不知道如何使用。
一种正确的程序外观:如何获得两个数相等或这个数更大的值? 在C++中,我正在经历一个在线课程,我不知道从哪里可以找到这个或好的C++网站的答案。 下面的代码应该给出输出:两个数字等于或大于a/b #include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { int a,b,c=0; cout<<"Enter value of 1st variable: "; cin>>a; cout<<"Enter value of 2nd variable: "; cin>>b; { if ( !(a == b)); !true; cout << "Both are equal"; } !false; { if (a>b) c = a; if (b>a); c = b; cout << "Greater value is " << c; } } #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { int a,b,c=0; 库塔; 库特布; { 如果(!(a==b)); 符合事实的 法院(b) c=a; 如果(b>a); c=b; CUT显然,如果C++中的其他方法正确,你就不知道如何使用。,c++,C++,一种正确的程序外观: #include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { int a, b, c = 0; cout << "Enter value of 1st variable: "; cin >> a; cout << "Enter value of
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
int a, b, c = 0;
cout << "Enter value of 1st variable: ";
cin >> a;
cout << "Enter value of 2nd variable: ";
cin >> b;
if (a == b){
cout << "Both are equal";
} else {
if (a > b) {
c = a;
} else if (a < b){ //<-- As indicated by @Thomas Matthews you don't actually need the third check here
c = b;
}
cout << "Greater value is " << c;
}
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
int a,b,c=0;
cout>a;
cout>b;
如果(a==b){
法院(b){
c=a;
否则,如果(a<b){//< p>您有几个基本C++语法问题。
if
语句通常不应以分号结尾:
if ( !(a == b));
应该是:
if ( !(a == b))
// /\ No semicolon here!
if (!(a == b))
{
}
if
语句的大括号应放在右括号之后。这是:
{
if (!(a == b))
}
应该是:
if ( !(a == b))
// /\ No semicolon here!
if (!(a == b))
{
}
这两条线都不起作用:
!true;
!false;
允许使用额外的大括号,这就是为什么您的编译器不会抱怨它们。仅仅因为我可以这样做,并不意味着我应该:
int main()
{
{
{
{
// code here
}
}
}
}
下面是一个简单的片段:
if (a == b)
{
cout << "a == b";
}
else
{
if (a < b)
{
cout << "a < b";
}
else
{
cout << "a >= b";
}
}
cout << "\n";
if(a==b)
{
CUT <代码>!TRUE;是一个NOP。请回到C++教科书的基本章节!<代码>(在两个IF)语句上加上分号。ῥεῖ 建议回到C++ C++的基本章节。学习if语句的C/C++ SNYAc税。这不是LISP,或者你从中得到的任何语法。因为你使用的语法并不意味着你认为它的意思。只是因为程序的语法是有效的,它并不意味着它是正确的。Python和C++与if语句非常相似,你知道有没有更有效的方法?@ Prrt'单词:在运行时效率或更少的代码行方面,有几种替代方法来实现它,但是我怀疑它们对性能有任何显著的影响。代码行(请参见编辑),但并非真正使用更少的代码。