2015-12-21 41 views
0

我编写的以下程序提示用户输入一个数字1 - 100.该程序有一个来自随机数发生器的预选号码,用户需要猜测。如果用户的猜测过高或过低,程序会通知用户,直到用户的猜测是正确的。我的程序运行良好,但是当提示输入一个数字时,我必须在某些情况下输入数字四次或更多次才能提示我。有人能让我看看或帮我找到我的错误吗?C++:提示需要多次输入数据才能继续

#include <iostream> 
#include <ctime>//A user stated that using this piece of code would make a true randomization process. 
#include <cstdlib> 
using namespace std; 

int main()//When inputing a number, sometimes the user has to input it more than once. 
{ 
    int x; 
    int ranNum; 
    srand(time(0)); 

    ranNum = rand() % 100 + 1; 

    cout << "Please input your guess for a random number between 1 - 100.\n"; 
    cin >> x; 

    while (x > ranNum || x < ranNum) 
    { 
     { 
      if (x > ranNum) 
       cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
      cin >> x; 
      if (x > 100 || x < 1) 
       cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      cin >> x; 
     } 
     { 
      if (x < ranNum) 
       cout << "Your input was less than the system's generated random number. Please try again.\n"; 
      cin >> x; 
      if (x > 100 || x < 1) 
       cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      cin >> x; 
     } 
     { 
      if (x == ranNum) 
       cout << "You guessed right!\n"; 
     } 
    } 

    return 0; 
} 
+1

'while(x> ranNum || x

回答

1

您的括号内的地方不对。一个如果具有多发线的说法应该是在

if (condition) 
{ 
    code1 
    code2 
    code3 
    ... 
} 

形式,你有什么是

{ 
    if condition 
     code1 
     code2 
     code3 
     ... 
} 

所以code1当条件是真实的,但code2code3其余的将运行将只运行不有什么关系。缩进不包括C++中的任何内容。我们可以有(请不要这样做):

  if (condition) 
    { 
code1 
code2 
     code3 
} 

而且所有三行将在if语句中运行。

您的代码修正为括号在正确的位置将是

while (x != ranNum) 
{ 
    if (x > ranNum) 
    { 
     cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
     cin >> x; 
     if (x > 100 || x < 1) 
     { 
      cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      cin >> x; 
     } 
    } 
    if (x < ranNum) 
    { 
     cout << "Your input was less than the system's generated random number. Please try again.\n"; 
     cin >> x; 
     if (x > 100 || x < 1) 
     { 
      cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      cin >> x; 
     } 
    } 
    if (x == ranNum) 
     cout << "You guessed right!\n"; 
} 

我也改变while (x > ranNum || x < ranNum)while (x != ranNum)因为我们只想要运行的循环,而x不等于ranNum

+0

正确,并且在嵌套条件中有一个类似的实例。 OP似乎期望缩进具有语义。 –

+0

感谢您的帮助。将括号放在正确的位置解决了我遇到的问题。 –

+0

@BillFisher没问题。如果这里或其他答案解决了这个问题,你可以考虑通过点击复选标记来接受它。这表明答案解决了您的问题。 – NathanOliver

0

你请勿正确使用if对帐单:

{ 
    if (x > ranNum) 
     cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
    cin >> x; 
    if (x > 100 || x < 1) 
     cout << "Input invalid. Please input a number between 1 - 100.\n"; 
    cin >> x; 
} 

Both 0上面的条件不会影响任何内容,但输出消息是否被打印,并且周围的花括号也是无用的。当空调的代码包含比单个语句多,你需要将其包装成花括号:

{ 
    if (x > ranNum) 
    { 
     cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
     cin >> x; 
    } 
    if (x > 100 || x < 1) 
    { 
     cout << "Input invalid. Please input a number between 1 - 100.\n"; 
     cin >> x; 
    } 
} 

也有在重复的代码读取输入并执行范围检查是没有意义的。下面看起来更直接对我说:

int main() 
{ 
    int ranNum; 
    srand(time(0)); 

    ranNum = rand() % 100 + 1; 

    cout << "Please input your guess for a random number between 1 - 100.\n"; 
    cin >> x; 

    int x; 
    for (;;) // Endless loop 
    {     
     cin >> x; 

     if (x > 100 || x < 1) 
     { 
      cout << "Input invalid. Please input a number between 1 - 100.\n"; 
      continue; // Jump to next iteration of the loop 
     }            

     if (x == ranNum)        
     { 
      cout << "You guessed right!\n"; 
      break; // Leave the loop 
     } 

     if (x > ranNum) 
      cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
     else 
      cout << "Your input was less than the system's generated random number. Please try again.\n"; 
    } 

    return 0; 
} 
0

您的代码看起来是这样的:

#include <iostream> 
#include <ctime>//A user stated that using this piece of code would make a true randomization process. 
#include <cstdlib> 
using namespace std; 

int main()//When inputing a number, sometimes the user has to input it more than once. 
{ 
    int x; 
int ranNum; 
srand(time(0)); 

ranNum = rand() % 100 + 1; 

cout << "Please input your guess for a random number between 1 - 100.\n"; 


while (x > ranNum || x < ranNum) 
{ 
    cin >> x; 
    if (x > ranNum && x<=100 & x>=1) cout << "Your input was greater than the system's generated random number. Please try again.\n"; 
    else if (x < ranNum && x<=100 & x>=1) cout << "Your input was smaller than the system's generated random number. Please try again.\n"; 
    else if (x>=100 || x<=1) cout << "Input invalid. Please input a number between 1 - 100.\n"; 

} 
cout << "You guessed right!\n"; 

    return 0; 
} 

在未来:

当您使用while循环,它包括if语句在里面。使用正确。另外,尝试nt来嵌套if语句,而不是使用逻辑运算符。

+0

在while循环条件下评估之前,您忘记了初始化x。 – NathanOliver

相关问题