【发布时间】:2012-11-04 15:46:57
【问题描述】:
我想要一些关于我在哪里出错的建议。控制台应用程序显示菜单选项并要求用户输入有效的菜单选项。
现在它的作用是,如果输入的数字是 1、2 或 3,它会显示“您已选择选项 x”然后“按任意键关闭”但程序不会显示“您已选择选项 x' 然后'按任意键关闭'。如果数字小于 1 或大于 3,它会起作用,说“菜单选择不在 1-3 之间”然后“请重新输入”。我哪里错了?
我已经很久没有编程了,如果我这次能纠正我的错误,我知道未来。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _4._6
{
class Program
{
static void Main(string[] args)
{
int iChoice = 0;
Console.WriteLine("1: Add two numbers");
Console.WriteLine("2: Multiply two numbers");
Console.WriteLine("3: Exit the program");
Console.WriteLine("Enter your choice: ");
iChoice = Convert.ToInt32(Console.ReadLine());
while (iChoice < 1 || iChoice > 3)
{
Console.WriteLine("Menu choice not between 1-3: ");
Console.Write("Please re-enter: ");
iChoice = Convert.ToInt32(Console.ReadLine());
Console.ReadKey();
if (iChoice > 1 || iChoice < 3)
{
Console.WriteLine("You have chosen option " + iChoice);
iChoice = Convert.ToInt32(Console.ReadLine());
Console.ReadKey();
}
}
}
}
}
【问题讨论】:
-
你能做的最好的事情就是学会使用调试器。然后你可以单步调试代码,看看发生了什么。
-
只有当 iChoice 不是 [1..3] 时才执行的 while 循环中的代码?在这种情况下,iChoice 将永远不会处于您的 if 语句指定的条件中。
标签: c#