我正在尝试学习c ++,我遇到了if/else语句的问题.当我在没有打开和关闭牙箍的情况下筑巢时,我以为我把它们弄下来了,但是我试着用牙箍来解决问题.
有人可以指出错误以及如何解决它,而不仅仅是答案; 这样我就不会学到东西了.
这是我的来源:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
signed long int RealNumber = 31710;
int Guess;
cout << "Lets see if you can guess my favorite number... \n";
cout << "Type in a number and hit enter to see if you have guessed it correctly. \n";
cout << "you should know this one Danielle. \n \n ";
cin >> Guess;
if (Guess == RealNumber)
{
cout << "Wow, you are amazing! \n";
cout << "Would you like to be punched?";
}
else
{
if (Guess < RealNumber)
cout << "The number is higher";
else
if (Guess > RealNumber)
cout << "The number is lower";
else
cout << "That is impossible!"; //trying to make sure that if anthing but a number is entered that the program doesn't crash.
}
}
char f; // used to make the program wait for input before closing.
cin >> f;
return 0;
Run Code Online (Sandbox Code Playgroud)
你的线上方有一个额外的支架,上面写着 -
char f;
cin >> f;
Run Code Online (Sandbox Code Playgroud)
这个右括号匹配你的主要功能的初始大括号.
你在if/else嵌套中使用的支撑结构对我来说似乎很好.
这是一个提示 - 对于每个左括号,立即键入右括号,并添加注释.你不必永远使用这个'拐杖',但作为一个学习编写代码的初学者,这个coudl非常有帮助.
步骤1:
int main()
{
}//main
Run Code Online (Sandbox Code Playgroud)
第2步:
int main()
{
int foo;
cin >> foo;
if (foo < 100)
{
//todo
}// if (foo < 100)
}//main
Run Code Online (Sandbox Code Playgroud)
第3步:
int main()
{
int foo;
cin >> foo;
if (foo < 100)
{
cout << "foo is too small";
cin >> foo;
if (foo < 100)
{
//todo
} // if (foo < 100), inner if statement
}// if (foo < 100)
}//main
Run Code Online (Sandbox Code Playgroud)
等等