Console.Redline阅读第二行

时间:2018-10-07 13:48:13

标签: c# console-application readline

我有一个简单的程序,告诉用户输入 n 名学生,为每个学生分配 x 名学生。最后,该程序将 x 除以 n ,这意味着总金额由学生平均分配。

问题是Console.Readline()正在读取第二个输入值,如下所示:

Console output

这意味着每次调用Console.Readline()时,用户都必须输入两次值,这显然是错误的!

代码:

static void Main(string[] args)
{
    double total = 0;
    int noOfStudents = 0;
    try
    {
        Console.WriteLine("Please enter the number of students :");
        noOfStudents = checkInputTypeInt(Console.ReadLine());
        Console.WriteLine("Enter the students(" + noOfStudents + ") money!");

        for (int i = 0; i <= noOfStudents - 1; i++)
        {
            double money = checkInputTypeDouble(Console.ReadLine());
            total += money;
        }
        double dividedTotal = total / noOfStudents;
        Console.WriteLine("Total divided by " + noOfStudents + " is $ " + dividedTotal.ToString("F2"));
        Console.ReadKey();
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
}

private static int checkInputTypeInt(string s)
{
    int numericValue = 0;
    bool done = false;
    while (!done)
    {
        if (!int.TryParse(Console.ReadLine(), out numericValue))
            Console.WriteLine("The input must be between 0 and 1000!");
        else if (numericValue > 100)
            Console.WriteLine("The input must be between 0 and 1000!");
        else
            done = true;
    }
    return numericValue;
}

2 个答案:

答案 0 :(得分:1)

您两次阅读热线:

noOfStudents = checkInputTypeInt(Console.ReadLine());

并使用checkInputTypeInt方法:

if (!int.TryParse(Console.ReadLine(), out numericValue))

您应该只将字符串发送给这样的方法:

  noOfStudents = checkInputTypeInt(Console.ReadLine());

,然后在您的方法中检查以下值:

if (!int.TryParse(s, out numericValue))

在这种情况下,您应该在主方法中使用while循环。

或仅在调用的方法中使用readline。

 Console.WriteLine("Please enter the number of students :");
 noOfStudents = checkInputTypeInt(); // and you should change your method to fit no arguments.

编辑:最终代码:

static void Main(string[] args)
{
    double total = 0;
    int noOfStudents = 0;
    try
    {
        Console.WriteLine("Please enter the number of students :");
        noOfStudents = checkInputTypeInt();
        Console.WriteLine("Enter the students(" + noOfStudents + ") money!");

        for (int i = 0; i <= noOfStudents - 1; i++)
        {
            double money = checkInputTypeDouble(Console.ReadLine());
            total += money;
        }
        double dividedTotal = total / noOfStudents;
        Console.WriteLine("Total divided by " + noOfStudents + " is $ " + dividedTotal.ToString("F2"));
        Console.ReadKey();
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
}

private static int checkInputTypeInt()
{
    int numericValue = 0;
    bool done = false;
    while (!done)
    {
        if (!int.TryParse(Console.ReadLine(), out numericValue))
            Console.WriteLine("The input must be between 0 and 1000!");
        else if (numericValue > 100)
            Console.WriteLine("The input must be between 0 and 1000!");
        else
            done = true;
    }
    return numericValue;
}

OR:

static void Main(string[] args)
{
    double total = 0;
    int noOfStudents = -1;
    try
    { 
        Console.WriteLine("Please enter the number of students :");
        do{
        noOfStudents = checkInputTypeInt(Console.ReadLine());
        }while(noOfStudents == -1);
        Console.WriteLine("Enter the students(" + noOfStudents + ") money!");

        for (int i = 0; i <= noOfStudents - 1; i++)
        {
            double money = checkInputTypeDouble(Console.ReadLine());
            total += money;
        }
        double dividedTotal = total / noOfStudents;
        Console.WriteLine("Total divided by " + noOfStudents + " is $ " + dividedTotal.ToString("F2"));
        Console.ReadKey();
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
}

private static int checkInputTypeInt(string s)
{
    int numericValue = -1;
        if (!int.TryParse(Console.ReadLine(), out numericValue))
            Console.WriteLine("The input must be between 0 and 1000!");
        else if (numericValue > 1000)
            {Console.WriteLine("The input must be between 0 and 1000!");
             numericValue = -1;
    }
    return numericValue;
}

答案 1 :(得分:0)

尝试像这样划分noOfStudentsConsole.ReadLine()

int i = Convert.ToInt32(Console.ReadLine());
noOfStudents = i;