C#在按下enter键之前,如何在循环询问相同问题的同时保持do
我是C#编程的初学者,尝试编写一个应用程序,使用do while循环读取用户输入,但是,当用户回答问题时,循环不会继续执行。我只希望循环在按下enter键时停止 这就是我到目前为止所做的 提前谢谢C#在按下enter键之前,如何在循环询问相同问题的同时保持do,c#,.net,visual-studio,constructor,C#,.net,Visual Studio,Constructor,我是C#编程的初学者,尝试编写一个应用程序,使用do while循环读取用户输入,但是,当用户回答问题时,循环不会继续执行。我只希望循环在按下enter键时停止 这就是我到目前为止所做的 提前谢谢 //call method to read a text, calculate and display the num of chars private void ShowStringLength() { ConsoleKeyInfo cki; do
//call method to read a text, calculate and display the num of chars
private void ShowStringLength() {
ConsoleKeyInfo cki;
do
{
Console.WriteLine("Let me calculate the length of strings for you!");
Console.WriteLine("Give me text of any length, or press ENTER to exit!");
Console.Write("Enter a string : ");
String str = Console.ReadLine();
Console.WriteLine(str.ToUpper());
int len = str.Length;
Console.WriteLine("Numer of Chars is : " + len);
//Reads “Enter” from the keyboard to exit the app
cki = Console.ReadKey();
Console.Write("OK. You pressed Enter. See you agian!");
} while (cki.Key != ConsoleKey.Enter);
我会试试看
if(Console.KeyAvailable)
{
cki = console.ReadKey();
...
}
cki=Console.ReadKey()代码>等待任何键输入,而不只是输入。这会显示文本“确定。您按了Enter键。再见!”即使您实际按了另一个键,也会显示该文本
您可以检查输入的字符串,如果该字符串为空,则退出循环。例如:
while (true)
{
Console.WriteLine("Let me calculate the length of strings for you!");
Console.WriteLine("Give me text of any length, or press ENTER to exit!");
Console.Write("Enter a string : ");
string str = Console.ReadLine();
// If the string is null, empty or only contains white spaces,
// break out of the loop.
if (string.IsNullOrWhiteSpace(str))
{
Console.Write("OK. You pressed Enter. See you agian!");
break;
}
Console.WriteLine(str.ToUpper());
Console.WriteLine("Numer of Chars is : " + str.Length);
};
你很接近:
private void ShowStringLength()
{
ConsoleKeyInfo cki;
do
{
Console.WriteLine("Let me calculate the length of strings for you!");
Console.WriteLine("Give me text of any length, or press ENTER to exit!");
Console.Write("Enter a string : ");
String str = Console.ReadLine();
Console.WriteLine(str.ToUpper());
int len = str.Length;
Console.WriteLine("Numer of Chars is : " + len);
//Reads “Enter” from the keyboard to exit the app
cki = Console.ReadKey();
if (cki.Key == ConsoleKey.Enter)
{
Console.Write("OK. You pressed Enter. See you agian!");
}
else
{
Console.WriteLine(); //To start with a clean line
}
} while (cki.Key != ConsoleKey.Enter);
}
我写了一个代码,让你的蚂蚁和我添加了一个字符;在这个样本空间中;
以确定字符串是否已完成。然后计算字符串的长度
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main()
{
Console.WriteLine("Press <Enter> to exit... ");
var outPutString = "";
var list = new List<int>();
while (true)
{
Console.WriteLine("Let me calculate the length of strings for you!");
Console.WriteLine("Give me text of any length, or press ENTER to exit!");
Console.Write("Enter a string End with space : ");
var readKey = Console.ReadKey().Key;
if (readKey == ConsoleKey.Enter)
{
Console.WriteLine(
$"Length of {outPutString} is : {(string.IsNullOrWhiteSpace(outPutString) ? 0 : outPutString.Length)}");
break;
}
while (readKey != ConsoleKey.Spacebar)
{
if (readKey == ConsoleKey.Enter)
{
Console.WriteLine(
$"Length of {outPutString} is : {(string.IsNullOrWhiteSpace(outPutString) ? 0 : outPutString.Length)}");
break;
}
var result = ((int) readKey);
list.Add(result);
outPutString = list.Aggregate(outPutString, (current, item) => current + (char) item);
list.Clear();
readKey = Console.ReadKey().Key;
}
Console.WriteLine(
$"Length of {outPutString} is : {(string.IsNullOrWhiteSpace(outPutString) ? 0 : outPutString.Length)}");
outPutString = "";
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间控制台EAPP1
{
班级计划
{
静态void Main()
{
控制台。WriteLine(“按退出…”);
var outPutString=“”;
var list=新列表();
while(true)
{
WriteLine(“让我为您计算字符串的长度!”);
WriteLine(“给我任何长度的文本,或者按ENTER键退出!”);
Write(“输入一个以空格结尾的字符串:”);
var readKey=Console.readKey().Key;
if(readKey==ConsoleKey.Enter)
{
控制台写入线(
$“{outPutString}的长度为:{(string.IsNullOrWhiteSpace(outPutString)?0:outPutString.Length)}”;
打破
}
while(readKey!=ConsoleKey.Spacebar)
{
if(readKey==ConsoleKey.Enter)
{
控制台写入线(
$“{outPutString}的长度为:{(string.IsNullOrWhiteSpace(outPutString)?0:outPutString.Length)}”;
打破
}
变量结果=((int)readKey);
列表。添加(结果);
outPutString=list.Aggregate(outPutString,(当前,项)=>current+(char)项);
list.Clear();
readKey=Console.readKey().Key;
}
控制台写入线(
$“{outPutString}的长度为:{(string.IsNullOrWhiteSpace(outPutString)?0:outPutString.Length)}”;
outPutString=“”;
}
}
}
}
你试过调试这个小应用程序吗?当用户在没有输入任何文本的情况下按Enter
时,你计算的len
将为零。因此,您可以将循环更改为while(true)
循环,如果(len==0)中断,则通过退出该循环代码>条件。@MatthewWatson谢谢Matthew,它和if条件一起工作,但是,我仍然得到这样的结果:给我任意长度的文本,或者按ENTER键退出!输入字符串:字符数为:0确定。你按了回车键。再见!按下回车键时,我希望它继续计数。谢谢您的回答!我现在可以退出循环,但是,应用程序仍然像这样计算长度:字符数是:0。我不希望在按下enter键时继续计算。我只想让应用程序打印出enter消息:“好的,你按了enter键。再见!”如果你使用了该示例,应用程序在按enter键后不会打印出“字符数为:0”。复制粘贴并重试。