C#-为什么我必须打两次Enter才能提交Console.Readline

时间:2020-03-30 19:44:37

标签: c#

C#新手。尝试制作一个简单的成绩簿程序,其中用户:

  1. 输入学生姓名,直到输入“完成”
  2. 输入每个用户的成绩,然后计算平均值

第2部分有效,但我的问题在于第1部分-您必须按两次Enter键才能将名称提交到列表中。例如,如果我输入Bob,Lisa,Kevin,Jane(只有Bob和Kevin可以进入),第二行(即使您键入了一些内容)也将console.read提交到列表中。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Csharp
{

    class MainClass
    {
        static List<string> mylist = new List<string> { };
        public static void Main(string[] args)
        {

            UserInput();
            GradeEnter();
        }
        public static void UserInput()
        {
            Console.WriteLine("Enter Some names (type 'done' when finished)");
            do
            {
                mylist.Add(Console.ReadLine());
            } while (!Console.ReadLine().Equals("done"));


        }
        public static void GradeEnter()
        {

            foreach (var x in mylist)
            {
                List<int> myInts = new List<int>();
                Console.WriteLine("\nEnter grades for {0}, (enter any letter when done)", x);
                while (Int32.TryParse(Console.ReadLine(), out int number))
                {
                    myInts.Add(number);
                }
                Console.Write("Average is ");
                Console.Write(myInts.Average());

            }
        }

    }
}

对此将提供任何帮助!

谢谢

3 个答案:

答案 0 :(得分:2)

您两次致电ReadLine。 您可以尝试以下方法:

public static void UserInput()
{
    Console.WriteLine("Enter Some names (type done to exit)");
    string name = Console.ReadLine();
    while (!name.Equals("done"));
    {
        mylist.Add(name);
        name = Console.ReadLine();
    } 
}

另一种相同的方法

public static void UserInput()
{
    Console.WriteLine("Enter Some names (type done to exit)");
    while (true);
    {
        string name = Console.ReadLine();
        if (name == "done")
        {
            // This will stop the while-loop
            break;
        } 
        mylist.Add(name);
    } 
}

现在让我们分析您的代码在做什么

        do
        {
            // Read line and add it to the list. Even if the user writes "done" 
            mylist.Add(Console.ReadLine());

        // Read the console again, if the user enters done, exit. But if the user enters other name, you are discarding it, you are not adding it to the list
        } while (!Console.ReadLine().Equals("done"));

一些使用您的代码的测试用例:

1. Peter <- gets added to the list
2. Lucas <- does not get added to the list, just checks if it is done
3. Mario <- gets added to the list
4. Juan  <- again, just checking if it is done, so not added to the list
5. done  <- It is treated like a name, so it will be added to the list
6. done  <- now it will finish :) 

答案 1 :(得分:1)

读取name 一次,然后将其添加到myList或停止循环:

public static void UserInput() {
  Console.WriteLine("Enter Some names (type done to exit)");

  for (string name = Console.ReadLine(); !name.Equals("done"); name = Console.ReadLine()) 
    mylist.Add(name);
}

答案 2 :(得分:0)

感谢大家的帮助。我最终使用了while(true)和一条if语句的组合:

Console.WriteLine("Enter some names (type 'done' when finished)");
            do
            {
                string name = Console.ReadLine();
                if (!name.Equals("done"))
                {
                    mylist.Add(name);
                }
                else
                    break;


            } while (true);