【发布时间】:2014-02-05 01:33:30
【问题描述】:
我让案例 1-3 正常工作,但我无法让案例 4-7 正常工作,或者不知道如何输入或处理它。
正如你在数字 4 中看到的那样,我编写了程序,所以当我运行程序并按 4 时,它应该运行 case 4 倒计时到 10 但跳过 7 为什么它不起作用?也不知道怎么做5-7。
我还没有学到那么远,如果可以帮助我并解释你的代码如何为我解决问题,那真的很合适。
namespace WhileLoopExercises
{
class Program
{
static void Main(string[] args)
{
int selection = 0;
while (selection != 9)
{
Console.Clear();
Console.WriteLine("\n\n Menu:\n");
Console.WriteLine("\t 1. Display 10 stars( one per line)");
Console.WriteLine("\t 2. Request a value 1 to 50 from user, then display");
Console.WriteLine("\t 3. Display 10 rows of 3 stars");
Console.WriteLine("\t 4. Add all values between 1 to 10 except 7");
Console.WriteLine("\t 5. Display all the even numbers between 10 and 100");
Console.WriteLine("\t 6. Add all the odd numbers bewteen 10 and 100");
Console.WriteLine("\t 7. Generate 6 random numbers ranging from 1 to 49");
Console.WriteLine("\t 8. Exit Apllication");
Console.Write("Enter your selection: ");
selection = int.Parse(Console.ReadLine());
switch (selection)
{
case 1:
{
int counter = 1;
while (counter <= 10)
{
Console.WriteLine("*");
counter++;
}
}
break;
case 2:
{
Console.Write("Enter integer 0-50 : ");
int N = int.Parse(Console.ReadLine());
int counter = 1;
while (counter <= N)
{
Console.Write("*");
counter++;
}
}
break;
case 3:
{
int counter = 1;
while (counter <= 10)
{
Console.WriteLine("***");
counter++;
}
}
break;
case 4:
{
int counter = 1;
while (counter <= 10)
{
if (counter == 7)
{
counter++;
}
}
}
break;
case 5:
{
int counter = 1;
while (counter <= 100)
{
}
}
break;
case 6:
{
}
break;
case 7:
{
}
break;
}// end of switch
//pause
Console.WriteLine("\n\nHit any key to coutinue");
Console.ReadKey();
}
}
}
}
【问题讨论】:
-
C# != C != C++。请仅选择适用于您的问题的语言标签,而不是随机抓取以第一个字母开头的语言标签。
-
语法如此接近有什么关系?我的意思是 if 语句
-
顺便说一句,如果这是作业,您可能需要将
"\n\nHit any key to coutinue"更正为"\n\nHit any key to continue";) -
@davidbro 等等有什么不同?不,它不是家庭作业,而是一种练习:)
-
coutinuevscontinue
标签: c# loops while-loop