【问题标题】:Testing if user input is between 1-100 not working?测试用户输入是否在 1-100 之间不起作用?
【发布时间】:2019-01-17 07:59:56
【问题描述】:

我正在调试一个介于 1-100 之间的“猜数字”程序。第一个用户创建一个 secretNumber,第二个用户猜测这个数字。

我正在尝试检查第二个用户输入是否是 1-100 之间的数字,但由于某种原因它仍然接受负数,我不知道为什么。

请注意,我对编码很陌生,所以我还不熟悉行话。在我的课上,我们刚刚学习了 while 循环和关系运算符以及 if/else 语句,所以我只能使用这些。

(secretNumber == 0) 是为了确保int.TryParse() 在用户输入字母而不是数字时不会返回 0。

if 语句检查:

( secretNumber >= 1 && secretNumber <= 100 )

还有

( secretNumber >= 1 || secretNumber <= 100 )

这是我的代码:

//Create a number variable to hold the whole number
int secretNumber;

//Validate that the number is a whole number AND between 1 and 100
if ( int.TryParse(secretNumberString, out secretNumber)
  || ( secretNumber >= 1 && secretNumber <= 100 )
  || secretNumber == 0 )
{

  // Tell the user the problem
  Console.WriteLine("Sorry, please only type in whole numbers and it must be between 1 and 100!");

  // Re-ask the question
  Console.WriteLine("What is your secret number?");

  // Re-catch the response in the same variable
  secretNumberString = Console.ReadLine();

  // Re-assign the new input
  int.TryParse(secretNumberString, out secretNumber);

} 

如果用户输入负值,则 if 语句应该运行。

如果用户输入的值大于 100,它就会运行,但如果值低于 100,它就不会运行。否则,如果数字在 1-100 之间正确,它就不应该运行。

【问题讨论】:

  • 另外,我已经尝试给我的老师发消息寻求帮助,但我猜他们已经睡着了。我很沮丧,因为我觉得这个问题的答案真的很简单,在搞砸了一个小时后我仍然弄错了......
  • if (int.TryParse(secretNumberString, out secretNumber) || 表示如果用户输入了一个实际数字 OR ... 你想要 AND
  • 有什么理由不使用 NumericUpDown 来避免整个问题?
  • @NoneoftheAbove 我只能使用我在课堂上学到的东西,而且我们只学习了布尔值、关系运算符和 if 语句。 ^^" 我还不知道那是什么。

标签: c#


【解决方案1】:

如果输入为非数字或超出 1~100 范围,则该语句必须为真。对吗?

//if string is NOT integral (notice the !)   Or less than 1      Or bigger than 100
if (!int.TryParse(str, out int secretNumber) || secretNumber < 1 || secretNumber > 100)

此外,由于未经验证,用户在第二次尝试时仍可以输入非整数、超出范围的数字。理想情况下,我认为最好将验证逻辑作为循环变量:

string input;
int secretNumber;
do
{
    Console.WriteLine("Input an integer between 1 and 100, inclusive.");
    input = Console.ReadLine();
}
while (!int.TryParse(input, out secretNumber) || secretNumber < 1 || secretNumber > 100);

【讨论】:

  • 我真的希望我可以在其他人的答案中添加评论...@TheGeneral 循环永远不会中断,因为str 在循环内不会改变。
  • 如果你想知道 do ... while () 循环:它相当于普通的 while 循环,除了在循环之后测试条件,这意味着块中的代码将至少执行一次。也称为测试后循环。
【解决方案2】:

问题是你的||(或),这只是一个简单的布尔代数(命题逻辑)错误,它必须是&amp;&amp;(和)

然而,一个类似的更简洁的模式是一个精简的验证循环

var num = 0;

Console.WriteLine("What is your secret number between 1 and 100 inclusively?");
var str = Console.ReadLine();

// while its not a number, "or" the number is less or equal to 0 "or" greater than 100
// annoy them with a message 
while (!int.TryParse(str, out num ) || num  <= 0 || num  > 100)
{
   Console.WriteLine("Omg you had one job, a number between 1 and 100 inclusively... Try again");
   str= Console.ReadLine()
}

Console.WriteLine($"You won, your number is {num } ");

【讨论】:

  • 感谢您的帮助!事实证明,这确实是一个逻辑错误。不过,当时我们还没有学习循环,只有 if 语句。
【解决方案3】:

您可能想阅读int.TryParse() 的文档——它返回true 如果字符串被成功解析为整数,否则返回false。你的测试,int.TryParse( secretNumberString, out secretNumber ) || ( secretNumber &gt;= 1 &amp;&amp; secretNumber &lt;= 100 ) || secretNumber == 0 ) 只会在解析失败时测试secretNumber 的值(此时它的值将是0)。

这可能会让你开始:

static int GetAnswer( string prompt, int min , int max )
{
  if (min > max) throw new ArgumentException("min must be less than or equal to max");
  int    value   ;
  bool   valid = false ;

  do
  {
    Console.WriteLine(prompt);
    string answer  = Console.ReadLine().Trim() ;

    valid =  int.TryParse(answer, out value)
          && value >= min
          && value <= max
          ;

    if (!valid)
    {
      Console.WriteLine($"Sorry, please only type in whole numbers in the range {min}-{max}.");
    }

  } while (!valid );

  return value;
}

【讨论】:

  • 感谢您的帮助!事实证明,这确实是一个逻辑错误。 :) 我设法弄清楚并按时完成了我的任务。
猜你喜欢
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
相关资源
最近更新 更多