Console.Redline 阅读第二行

Console.Redline reading second line

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

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

这意味着用户必须输入两次值,每次调用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;
}

你ReadLine两次:

noOfStudents = checkInputTypeInt(Console.ReadLine());

并在 checkInputTypeInt 方法中:

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

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

  noOfStudents = checkInputTypeInt(Console.ReadLine());

并在您的方法中检查该值,如:

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

在这种情况下,您应该在 main 方法中使用 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;
}

或:

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;
}

尝试这样划分 noOfStudentsConsole.ReadLine()

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