C# 我有一个带if语句的哨兵while循环,我想在不中断while循环的情况下中断if语句#
我应该只使用哨兵while循环来完成任务。用户需要输入一个部门代码,对于每个部门代码,应该输入一个标记,然后在结束时退出while循环,计算平均值。我的问题是,我能在while循环中中断If语句而不中断while循环本身吗?一旦按下Q,退出程序C# 我有一个带if语句的哨兵while循环,我想在不中断while循环的情况下中断if语句#,c#,C#,我应该只使用哨兵while循环来完成任务。用户需要输入一个部门代码,对于每个部门代码,应该输入一个标记,然后在结束时退出while循环,计算平均值。我的问题是,我能在while循环中中断If语句而不中断while循环本身吗?一旦按下Q,退出程序 Console.WriteLine("Enter a department code: ‘C’ or ‘c’ for Computer Science,‘M’ or " + "
Console.WriteLine("Enter a department code: ‘C’ or ‘c’ for Computer Science,‘M’ or " +
"‘m’ for Math, ‘B’ or ‘b’ for Business Admin, or enter ‘Q’ or ‘q’ to quit => C");
char deptCode = Console.ReadKey().KeyChar;
while (char.ToUpper(deptCode) != 'Q')
{
if (char.ToUpper(deptCode) == 'C')
{
Console.WriteLine("\nEnter the mark (>= 0 or <= 100)");
computerScienceMark = Convert.ToInt32(Console.ReadLine());
break;
}
else if (char.ToUpper(deptCode) == 'B')
{
Console.WriteLine("\nEnter the mark (>= 0 or <= 100)");
businessMark = Convert.ToInt32(Console.ReadLine());
break;
}
else if (char.ToUpper(deptCode) == 'M')
{
Console.WriteLine("\nEnter the mark (>= 0 or <= 100)");
mathMark = Convert.ToInt32(Console.ReadLine());
break;
}
if (char.ToUpper(deptCode) == 'Q')
{
Console.WriteLine("you entered Q to quit");
Environment.Exit(0);
}
else
{
Console.WriteLine("\nYou entered a wrong input please try again");
break;
}
}
Console.WriteLine(“输入部门代码:'C'或'C'表示计算机科学,'M'或”+
“'m'表示数学,'B'或'B'表示业务管理,或输入'Q'或'Q'表示退出=>C”);
char deptCode=Console.ReadKey().KeyChar;
while(char.ToUpper(deptCode)!='Q')
{
if(char.ToUpper(deptCode)='C')
{
Console.WriteLine(“\n输入标记(>=0或=0或=0或您可以在一段时间内输入整个部门代码请求。
这样,用户将被要求输入一个新代码,直到他们输入“Q”,如下所示
while (true)
{
Console.WriteLine("Enter a department code: ‘C’ or ‘c’ for Computer Science,‘M’ or ‘m’ for Math, ‘B’ or ‘b’ for Business Admin, or enter ‘Q’ or ‘q’ to quit => C");
char deptCode = Console.ReadLine()[0];
if (char.ToUpper(deptCode) == 'Q')
break;
if (char.ToUpper(deptCode) == 'C')
{
Console.WriteLine("\nEnter the mark (>= 0 or <= 100)");
computerScienceMark = Convert.ToInt32(Console.ReadLine());
}
else if (char.ToUpper(deptCode) == 'B')
{
Console.WriteLine("\nEnter the mark (>= 0 or <= 100)");
businessMark = Convert.ToInt32(Console.ReadLine());
}
else if (char.ToUpper(deptCode) == 'M')
{
Console.WriteLine("\nEnter the mark (>= 0 or <= 100)");
mathMark = Convert.ToInt32(Console.ReadLine());
}
else
{
Console.WriteLine("\nYou entered a wrong input please try again");
}
}
while(true)
{
Console.WriteLine(“输入部门代码:计算机科学为'C'或'C',数学为'M'或'M',商业管理为'B'或'B',或输入'Q'或'Q'退出=>C”);
char deptCode=Console.ReadLine()[0];
if(char.ToUpper(deptCode)='Q')
打破
if(char.ToUpper(deptCode)='C')
{
Console.WriteLine(“\n输入标记(>=0或=0或=0或您需要两个循环。一个外部循环获取部门代码,一个内部循环获取标记。大致如下:
static void Main(string[] args)
{
while (true) // outer loop
{
string department;
Console.WriteLine("Enter department code:");
department = Console.ReadLine().ToUpper();
if (department == "Q")
{
break; // end the outer loop
}
else if (department == "C"
|| department == "B"
|| department == "M")
{
while (true) // inner loop
{
int mark;
Console.WriteLine("Enter mark:");
if (int.TryParse(Console.ReadLine(), out mark)
&& mark >= 0
&& mark <= 100))
{
break; // end the inner loop;
}
else
{
Console.WriteLine("Invalid mark. Try again.");
}
}
}
else
{
Console.WriteLine("Invalid department code. Try again.");
}
}
}
static void Main(字符串[]args)
{
while(true)//外循环
{
弦乐部;
Console.WriteLine(“输入部门代码:”);
department=Console.ReadLine().ToUpper();
如果(部门=“Q”)
{
break;//结束外部循环
}
否则,如果(部门==“C”
||部门==“B”
||部门==“M”)
{
while(true)//内部循环
{
整数标记;
Console.WriteLine(“输入标记:”);
if(int.TryParse(Console.ReadLine(),out-mark)
&&标记>=0
&&标记bool check=false;
while(check==false)
{
Console.WriteLine(“输入部门代码:'C'或'C'表示计算机科学,'M'或'M'”+
“对于数学,“B”或“B”表示业务管理,或输入“Q”或“Q”表示退出=>C”);
char deptCode=Console.ReadLine()[0];
if(char.ToUpper(deptCode)='Q')
{
检查=正确;
}
Console.WriteLine(“\n输入标记(>=0或“我是否可以在while循环中中断If语句而不中断while循环?”“--这是什么意思?也许可以用注释来增加代码,说明在何种情况下从何处跳到何处,以使您的意图更清楚。很抱歉,我本应该更清楚地说明这一点,但我的意思是,我希望代码要求一个部门代码(在显示的代码上方要求)。然后,当用户输入C、B、M时,它将要求用户输入一个标记。一旦输入了一个标记,它应再次要求输入一个部门代码。如果有不相关的建议,请不要在elseif块之间加上白色。Allman样式已经提供了足够的空间来清楚地识别不同的blocks@stickybit好的,我会的e你的建议,我会编辑这个问题,你是对的,切换更清晰,就像在切换前写一次标记请求一样。我使用了if-else和重复的控制台写入,因为OP这样做了,我想提供一个尽可能接近他的代码的解决方案。此外,中断和继续没有问题。它们是基金在循环处理中,即使是goto也非常有用,尤其是在C语言中,当你需要一个单独的退出点来完成一些清理时
bool check = false;
while (check == false)
{
Console.WriteLine("Enter a department code: ‘C’ or ‘c’ for Computer Science,‘M’ or ‘m’ " +
"for Math, ‘B’ or ‘b’ for Business Admin, or enter ‘Q’ or ‘q’ to quit => C");
char deptCode = Console.ReadLine()[0];
if (char.ToUpper(deptCode) == 'Q')
{
check = true;
}
Console.WriteLine("\nEnter the mark (>= 0 or <= 100)");
switch (char.ToUpper(deptCode))
{
case 'C':
computerScienceMark = Convert.ToInt32(Console.ReadLine());
check = true;
break;
case 'B':
businessMark = Convert.ToInt32(Console.ReadLine());
check = true;
break;
case 'M':
mathMark = Convert.ToInt32(Console.ReadLine());
check = true;
break;
default:
Console.WriteLine("\nYou entered a wrong input please try again");
break;
}
}