循环中的错误答案 [英] wrong answer in loop
本文介绍了循环中的错误答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Menu
{
public void go()
{
int choice = -1;
while (choice != 0)
{
WriteMenuText();
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
{
}
case 2:
{
}
case 3:
{
}
}
}
}
public void WriteMenuText()
{
Console.WriteLine("-------------------------------------------------------");
Console.WriteLine(" week :1");
Console.WriteLine(" Time :2");
Console.WriteLine(" Currency :3");
Console.WriteLine(" calculater :4");
Console.WriteLine(" Exit :0");
Console.WriteLine("-------------------------------------------------------");
Console.WriteLine("\nYour choice");
}
}
}
我有一个白色的问题,当我要选择一个大于4的数字,而小于0时,它会说错误的数字,然后重试,菜单将耗费一个摩尔时间.但是我不起作用.
I have one problem white it when im going to pick a number over 4 and under 0 its going to say wrong nummber try again and the menu will com up one moore time. but i dont get it to work.
推荐答案
在切换情况下最好使用
In switch case better to use
default :
就您而言,就是这样
In your case,it is like this
default :Console.WriteLine("Entered wrong choice");
并像这样更改您的代码
and change your code some thing like this
WriteMenuText();
choice = int.Parse(Console.ReadLine());
while (choice != 0)
{
switch (choice)
{
case 1:
{
}
case 2:
{
}
case 3:
{
}
case 4:
{
}
default: { }
}
}
谢谢
SP
Thanks
SP
您的时间仅检查0.保持循环运行.
Your while is checking for 0 only. Anything else keeps the loop running.
修改go函数并更改开关大小写以添加默认大小写.
代码如下.
modify your go function and change the switch case to add the default case.
code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Menu
{
public void go()
{
int choice = -1;
while (choice != 0)
{
WriteMenuText();
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
{
}
case 2:
{
}
case 3:
{
}
default:
{
Console.WriteLine("Wrong Number");
}
}
}
}
public void WriteMenuText()
{
Console.WriteLine("-------------------------------------------------------");
Console.WriteLine(" week :1");
Console.WriteLine(" Time :2");
Console.WriteLine(" Currency :3");
Console.WriteLine(" calculater :4");
Console.WriteLine(" Exit ");
Console.WriteLine("-------------------------------------------------------");
Console.WriteLine("\nYour choice");
}
}
}
如有任何问题,请还原.
in case of any problem revert back.
这篇关于循环中的错误答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文