如何通过按键退出for循环? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

对于我的ASSIGNMENT,我正在运行这个循环来收集id#和2d数组中的名字。但是如果用户不想坐下并输入30组数字和名字,我希望能够突破循环。因为......他们为什么会这样?!

string[,] studentNames = new string[30,30];

// Accepting value from user 
for (i = 0; i < 30; i++)
{
    Console.Write("\nEnter id #, Student Name :\t");

    //Storing value in an array
    studentNames[i, 0] = (Console.ReadLine());
}

我试过添加:

do
{
 //the stuff
} while(Console.ReadKey().Key != ConsoleKey.Escape);

但这不起作用。

c# do-while
1个回答
4
投票

你可以使用这样的东西:

readLine = (Console.ReadLine());
if(readLine == "exit") break;
studentNames[i, 0] = readLine;

如果用户输入单词exit,那将退出。根据语言,比较readLine == "exit"可能必须被一些比较功能的字符串替换。

我希望它有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.