C#--为什么我必须按两次enter键才能提交Console.Readline
新手。尝试制作一个简单的成绩册程序,其中用户:C#--为什么我必须按两次enter键才能提交Console.Readline,c#,C#,新手。尝试制作一个简单的成绩册程序,其中用户: 输入学生姓名,直到输入“完成” 输入每个用户的分数,然后计算平均值 第2部分是可行的,但我的问题是第1部分--必须按两次enter键才能将名称提交到列表中。例如,如果我输入Bob、Lisa、Kevin、Jane——只有Bob和Kevin会进入——第二行(即使您键入了一些内容)充当console.read提交到列表的行 这是我的密码: using System; using System.Collections.Generic; using Syst
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());
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
命名空间Csharp
{
类主类
{
静态列表mylist=新列表{};
公共静态void Main(字符串[]args)
{
用户输入();
GradeEnter();
}
公共静态void UserInput()
{
WriteLine(“输入一些名称(完成时键入'done'”);
做
{
添加(Console.ReadLine());
}而(!Console.ReadLine().Equals(“done”);
}
公共静态void GradeEnter()
{
foreach(mylist中的变量x)
{
List myInts=新列表();
Console.WriteLine(“\n输入{0}的分数,(完成后输入任何字母)”,x);
while(Int32.TryParse(Console.ReadLine(),out int number))
{
myInts.Add(数字);
}
控制台。写入(“平均值”);
Write(myInts.Average());
}
}
}
}
在此方面的任何帮助都将不胜感激
谢谢你给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。Peter您正在给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。Peter阅读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);
}
阅读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);
}
谢谢大家的帮助。我最终使用了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);
谢谢大家的帮助。我最终使用了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);
每次循环迭代调用Console.ReadLine()
两次。在调试器中单步执行并观察它的发生。Madreflection是正确的。问题是,你正在阅读你认为自己编写的程序,而不是阅读你实际编写的程序。如果你把你的程序分解成每个语句都有一个操作,那么这里发生的事情就会变得更清楚。我喜欢你在这个程序中使用函数抽象将它分解成逻辑部分的方式。继续这样做。例如,假设您有一个从控制台读取数字的函数;然后,您可以将该函数设计为返回一个可为null的int,而不是使用out参数,然后程序的其余部分将变得更易于读取。类似地,您是否可以让UserInput
返回一个列表,而不是改变现有列表?while中的读线不是重复上一行,而是读取一个新的行,您可以为循环的每次迭代调用Console.ReadLine()
两次。在调试器中单步执行并观察它的发生。Madreflection是正确的。问题是,你正在阅读你认为自己编写的程序,而不是阅读你实际编写的程序。如果你把你的程序分解成每个语句都有一个操作,那么这里发生的事情就会变得更清楚。我喜欢你在这个程序中使用函数抽象将它分解成逻辑部分的方式。继续这样做。例如,假设您有一个从控制台读取数字的函数;然后,您可以将该函数设计为返回一个可为null的int,而不是使用out参数,然后程序的其余部分将变得更易于读取。同样,你能让UserInput
返回一个列表而不是改变现有列表吗?你的while中的ReadLine不是重复上一行而是读取一个新的,或者更好,不要重复ReadLine
。只要while(true){string name=Console.ReadLine();if(name==“done”)break;…}
就可以避免重复。非常感谢。我走的路线是做一段时间(True)和一个If语句。感谢所有的反馈!或者更好的是,不要复制读线
。只要while(true){string name=Console.ReadLine();if(name==“done”)break;…}
就可以避免重复。非常感谢。我走的路线是做一段时间(True)和一个If语句。感谢所有的反馈!对于这种while语句,这是我最喜欢的技巧;一个很小的改进是while(true)body代码>比do{body}更容易阅读,而(true)
这是我最喜欢的while语句技巧;一个很小的改进是while(true)body代码>比do{body}更容易阅读,而(true)代码>