【问题标题】:Can't reach if statement无法到达 if 语句
【发布时间】:2018-08-16 10:36:54
【问题描述】:

您好,我正在用 C# 编写一个程序,它接受一个数字并要求用户输入一个数字,一旦用户输入数字,程序就会检查它是否与原始数字匹配,如果不匹配,它应该告诉用户他们的答案小于或大于,但是我的 if 语句不起作用。

        //Boolean if user passes
        Boolean Userpassed;
        // Declares Answer Variable
        int answer = 6;
        // Asks user for a number
        Console.WriteLine("Enter a number between 1 and 10");
        // Displays Message
        Console.ReadLine();
        // Takes User Input as String
        string useranswer = Console.ReadLine();
        // Declares integer for actual integer
        int userinput;
        // User string parsed to Int
        userinput = Convert.ToInt32(Console.Read());
        // Displays Message
        Console.ReadLine();
        // Checks if input is equal to answer
        if (userinput == answer)
        {
            // User passes
            Console.WriteLine("Congratulations, you have passed");

        }
        // Checks if input is greater than answer
        else if (userinput > answer)
        {
            // User Fails
            Console.WriteLine("The actual answer is less than what you entered");
        }
        // Checks if input is less than answer
        else if (userinput < answer)
        {
            // User Fails
            Console.WriteLine("The actual answer is greater than what you entered");
        }
        // Displays Message
        Console.ReadLine();

第一个 if 语句不起作用,因为当我输入 6 时,它只是传递给 else 循环。然后,如果我输入不同的数字,它不会超过第二个 else 循环。我该如何解决这个问题?

【问题讨论】:

  • 为什么不将useranswer 转换为userinputConsole.Read 也不会返回您认为的结果。而且你有很多ReadLines。用户必须按 Enter 3 次才能到达 if
  • @juharr 当我转换 useranswer 时它说我无法将字符串转换为整数
  • @ProTalz int user input = Int.Parse(Console.ReadLine()); TryParse 更好,但评论太长了
  • userinput = Convert.ToInt32(useranswer) 不起作用?你得到什么错误?请注意,如果您只是使用Console.Read 并按 6 键它不会返回 6,它会返回 54 字符 6 的 Ascii 值。
  • 您似乎对Console.ReadConsole.ReadLine 的含义感到困惑。 Console.Read 从控制台读取并返回一个字符。 Console.ReadLine 读取一行字符(直到用户按下回车键)并将它们作为字符串返回。只需使用类似这样的东西:var userinput = Convert.ToInt32(Console.ReadLine()) 从标准输入中读取一个整数。

标签: c# if-statement random


【解决方案1】:

您输入的Console.ReadLine(); 比您需要的多

无需对代码进行更多更改

 // Boolean if user passes
 Boolean Userpassed;
 // Declares Answer Variable
 int answer = 6;
 // Asks user for a number
 Console.WriteLine("Enter a number between 1 and 10");
 // Displays Message
 // Console.ReadLine();       <== Remove this line
 // Takes User Input as String
 string useranswer = Console.ReadLine();
 // Declares integer for actual integer
 int userinput;
 // User string parsed to Int
 userinput = Convert.ToInt32(useranswer);       //<== pass useranswer to here
 // Displays Message
 // Console.ReadLine();    <== Remove this line
 // Checks if input is equal to answer

 //your rest code is same here

【讨论】:

    【解决方案2】:

    别担心你会学到这些东西

    int ans = 6;
    Console.WriteLine("Enter a number between 1 and 10");
    //taking integer input from user
    int input = Convert.ToInt32(Console.ReadLine());
    //now checking user input with selected answere
    if(ans == input)//true, when user predicts correct number
    {
        Console.WriteLine("Congratulations!!!");
    }
    else if(input < ans)//true, when user predicted number < actual number
    {
        Console.WriteLine("The actual answer is less than what you entered");
    }
    else//true, when both above conditions are false
    {
        Console.WriteLine("The actual answer is greater than what you entered");
    }
    

    【讨论】:

      【解决方案3】:

      你必须Parse输入line而不是Convert一个单个字符,即代替

       Console.WriteLine("Enter a number between 1 and 10");
       // Displays Message
       // Console.ReadLine();       <== Remove this line
       // Takes User Input as String
       string useranswer = Console.ReadLine();
       // Declares integer for actual integer
       int userinput;
       // User string parsed to Int
       userinput = Convert.ToInt32(useranswer);
      

      你应该这样写:

       int userinput = 0;
      
       // Keep on asking until valid input provided - an integer number in [1..10] range
       while (true) {
         Console.WriteLine("Enter a number between 1 and 10");
      
         if (!int.TryParse(Console.ReadLine(), out userinput))
           Console.WriteLine("Sorry, syntax error; try again");
         else if (userinput < 0)
           Console.WriteLine("Your input less than 0; try again");  
         else if (userinput > 10)
           Console.WriteLine("Your input greater than 10; try again");  
         else
           break; // <- Valid input
       } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-31
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        • 2016-11-09
        • 1970-01-01
        • 1970-01-01
        • 2017-04-29
        相关资源
        最近更新 更多