如何使用Readline输入编写IF / ELSE语句?

时间:2016-02-02 18:01:39

标签: c#

我正在尝试编写一个接受用户数字输入然后输出预定消息的程序。我的问题是在将用户输入从字符串转换为int之后,如何在IF / ELSE语句中使用它们的输入。

这是我到目前为止所拥有的:

string UserInput;             Console.Write(“输入一个随机数?”);

        UserInput =Console.ReadLine();
        int x = Convert.ToInt32 (UserInput);
        Console.WriteLine (" You entered: " + UserInput);

        int x;

        if (x < 0) 
        {
            Console.WriteLine (" Error message: Out of range: Enter a number between 0 and 200");
        }

        { else if (x >100)

            Console.WriteLine (" You are above average");
        }

        { else if (x == 100)

            Console.WriteLine (" You are average");
        }

        {
            else if (x < 100)

            Console.WriteLine (" Sorry but you are below average");
        }

3 个答案:

答案 0 :(得分:0)

else语句的左括号应该在else之后的行上,而不是在它之前。您的初始if语句还需要检查&gt;根据印刷的信息,也是200。

您也可以使用Int32.TryParse方法添加错误检查。

string UserInput; 
int x;
Console.Write ("Enter a random number? ");
UserInput =Console.ReadLine();
Console.WriteLine (" You entered: " + UserInput);

if (! Int32.TryParse(UserInput, out x))
  {
     Console.WriteLine ("Invalid data input");
  }
else if ((x < 0) || (x>200))
    {
        Console.WriteLine (" Error message: Out of range: Enter a number between 0 and 200");
    }
else if (x >100)
    {
        Console.WriteLine (" You are above average");
    }
else if (x == 100)
    {
        Console.WriteLine (" You are average");
    }
else if (x < 100)
    {
        Console.WriteLine (" Sorry but you are below average");
    }

答案 1 :(得分:0)

让我建议一种方法。这种方式更清晰,包含错误处理。我认为你应该通过一个C#教程。欢呼声。

    public static void Main(string[] args)
    {
        Console.Write("Enter a random number? ");
        string userInput = Console.ReadLine();
        Console.WriteLine(" You entered: " + userInput);

        try
        {
            int input = int.Parse(userInput);
            PrintMessage(input);
        }
        catch (Exception)
        {
            Console.WriteLine(" Error message: Your input is not a number");
        }
    }

    private static void PrintMessage(int input)
    {
        if (input < 0)
        {
            Console.WriteLine(" Error message: Out of range: Enter a number between 0 and 200");
        }
        else if (input > 100)
        {
            Console.WriteLine(" You are above average");
        }
        else if (input == 100)
        {
            Console.WriteLine(" You are average");
        }
        else
        {
            Console.WriteLine(" Sorry but you are below average");
        }
    }

答案 2 :(得分:0)

您是否尝试直接输入该功能:

    if (Convert.ToInt32 (UserInput) < 0) 
    {
        Console.WriteLine (" Error message: Out of range: Enter a number between 0 and 200");
    }
    else if Convert.ToInt32 (UserInput) >100) {

        Console.WriteLine (" You are above average");
    }

    else if (Convert.ToInt32 (UserInput) == 100) {

        Console.WriteLine (" You are average");
    }

    else if (Convert.ToInt32 (UserInput) < 100) {

        Console.WriteLine (" Sorry but you are below average");
    }