![](/img/trans.png)
[英]Do-While Loops but the values for the while statement is inside the Do Statement
[英]Stopping a line of execution after it has been excuted in a do-while statement
我正在上课,老师问我们是否可以解决这个问题。 我已经看了几个小时,却找不到方法。
目的是使显示displaymenu
仅显示一次。 该应用程序循环运行,因此您可以重复使用它而无需退出。 显示displaymenu
向用户显示选择他们想要做什么的选项。 现在,我认为这不是大家都看过的最干净的代码,但我仍在学习-仅仅做了一周。 任何其他建议,将不胜感激。
static void Main(string[] args)
{
string choice = "";
do {
**displayMenu();** // only want to display once
choice = getChoice();
}
while (choice != "10");
{
Console.ReadLine();
}
}
static void displayMenu()
{
Console.WriteLine("Which shape do you want to work with?");
Console.WriteLine("_____________________________________");
Console.WriteLine("Press 1 for a circle.");
Console.WriteLine("Press 2 for an equilateral triangle.");
Console.WriteLine("Press 3 for a square.");
Console.WriteLine("Press 4 for a pentagon.");
Console.WriteLine("Press 5 for a hexagon.");
Console.WriteLine("Press 6 for a heptagon.");
Console.WriteLine("Press 7 for a octagon.");
Console.WriteLine("Press 8 for a nonagon.");
Console.WriteLine("Press 9 for a decagon.");
Console.WriteLine("Press 10 to quit.");
}
static string getChoice()
{
string c = Console.ReadLine();
if (c == "1")
circle();
if (c == "2")
triangle();
if (c == "3")
square();
if (c == "4")
polygon(5);
if (c == "5")
polygon(6);
if (c == "6")
polygon(7);
if (c == "7")
polygon(8);
if (c == "8")
polygon(9);
if (c == "9")
polygon(10);
return c;
}
如果要显示一次,是否将其放在循环之外?
static void Main(string[] args)
{
string choice = "";
displayMenu();
do {
choice = getChoice();
}
while (choice != "10");
{
Console.ReadLine();
}
}
由于选择是数字,所以使用整数作为输入会更好吗?
static void Main(string[] args)
{
do
{
choice = getChoice();
}
while (choice != 10);
{
Console.ReadLine();
}
}
要将字符串转换为int可能很容易:
int choice = int.Parse(Console.ReadLine());
但是,如果输入的不是数字,则会产生错误。 因此,这是首选:
static void Main(string[] args)
{
bool isInt;
int intNumber;
int choice;
string stringInput = Console.ReadLine();
isInt = int.TryParse(stringInput, out intNumber);
if (!isInt)
{
Console.WriteLine("Input is not a number");
}
else
{
choice = intNumber;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.