c#中嵌套While语句的无穷循环>> [

问题描述 投票:0回答:1
我正在尝试为C#控制台程序利用2个while循环。外部是重复程序菜单和功能,直到用户退出。我正在使用内部循环作为用户输入的验证(需要将其解析为int)。

内部循环似乎可以正常工作,但是一旦程序进入switch语句,它似乎会无限循环循环切换条件。因此,我相信外部的while循环使它不断地写入,而不是一次写入控制台。只有将flag设置为false的情况才会按预期停止。

有人可以解释发生了什么吗?我将如何更新标志,以便外部while循环不会无休止地执行开关中的功能?在所有情况下,程序都应返回到用户菜单并等待输入,除非用户选择7结束程序。我已经盯着这个看了好几个小时了。请指导我!

using System; using static System.Console; using System.Collections.Generic; namespace Project02AreaCalculator { class Program { static private bool flag = true; static void Main(string[] args) { string inputvalue = ""; int caseSwitch; // repeat program until flag is set to false by user selecting 7 to exit do { // repeat menu until user input can be parsed into an integer while (!int.TryParse(inputvalue, out caseSwitch)) { flag = false; Console.WriteLine("Shape Area Calculator"); Console.WriteLine("******************************************"); Console.WriteLine("\t1. Circle"); Console.WriteLine("\t2. Square"); Console.WriteLine("\t3. Rectangle"); Console.WriteLine("\t4. Rhombus"); Console.WriteLine("\t5. Parallelogram"); Console.WriteLine("\t6. Trapezoid"); Console.WriteLine("\t7. Exit"); Console.WriteLine("******************************************"); Console.WriteLine("Select a shape type to calculate"); inputvalue = ReadLine(); } //convert input to integer and assign to caseSwitch caseSwitch = Convert.ToInt32(inputvalue); switch (caseSwitch) { case 1: CalculateCircle(); //Clear(); flag = true; break; case 2: CalculateSquare(); // Clear(); flag = true; break; case 3: CalculateRectangle(); //Clear(); flag = true; break; case 4: CalculateRhombus(); //Clear(); flag = true; break; case 5: CalculateParallelogram(); //Clear(); flag = true; break; case 6: CalculateTrapezoid(); //Clear(); flag = true; break; case 7: //Clear(); Console.WriteLine("Goodbye! Press any key to end"); ReadKey(); flag = false; break; default: // Clear(); Console.WriteLine("Invalid Selection, Select 1-7"); flag = true; break; } } while (caseSwitch != 7); }//end main //****************METHODS SECTION **************************************************************************** static private void CalculateCircle() { //area = pi * radius * radius (or pi times radius squared) Console.Clear(); Console.WriteLine("Circle Area Calculator"); Console.WriteLine("Enter the length of the circle's radius (-1 to exit back to menu):"); Console.ReadKey(); } static private void CalculateSquare() { //area = side * side (or side squared) Console.Clear(); Console.WriteLine("Square Area Calculator"); Console.WriteLine("Enter the length of one side of the square (-1 to exit back to menu):"); Console.ReadKey(); } static private void CalculateRectangle() { //area = Length * Width Console.Clear(); Console.WriteLine("Rectangle Area Calculator"); Console.WriteLine("Enter the length of one side of the Rectangle (-1 to exit back to menu):"); Console.WriteLine("Enter the length of an opposing side of the Rectangle (-1 to exit back to menu):"); Console.ReadKey(); } static private void CalculateRhombus() { //area = ½ a * b (a and b being diagonals) Console.Clear(); Console.WriteLine("Rhombus Area Calculator"); Console.WriteLine("Enter the length of one diagonal of the rhombus (-1 to exit back to menu):"); Console.WriteLine("Enter the length of the other diagonal of the rhombus (-1 to exit back to menu):"); Console.ReadKey(); } static private void CalculateParallelogram() { //area = base * height Console.WriteLine("Parallelogram Area Calculator"); Console.WriteLine("Enter the length of the base of the Parallelogram (-1 to exit back to menu):"); Console.WriteLine("Enter the height of the Parallogram (-1 to exit back to menu):"); Console.ReadKey(); } static private void CalculateTrapezoid() { //area = ½ height * (largeBase + smallBase) Console.WriteLine("Trapezoid Area Calculator"); Console.ReadKey(); } } }

我正在尝试为C#控制台程序利用2个while循环。外部是重复程序菜单和功能,直到用户退出。我正在使用内部循环作为用户输入的验证(其中...
c# while-loop
1个回答
0
投票
尝试添加inputvalue =“”;后caseSwitch = Convert.ToInt32(输入值);
© www.soinside.com 2019 - 2024. All rights reserved.