首页 > 解决方案 > Console.Redline 读取第二行

问题描述

我有一个简单的程序,告诉用户输入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;
}

标签: c#console-applicationreadline

解决方案


你读了两次:

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

推荐阅读