问题描述
在我的 while 循环中,它会提示用户输入一个值,一旦输入值,就会运行某些方法.我遇到的问题是提示用户输入值两次(即要求输入 A、B 或 X.用户输入值然后程序再次要求输入 A、B 或 X,而没有执行正确的操作).输入后对其操作的任何帮助都会很棒!
In my while loop it prompts the user to enter a value and once value is entered operates certain methods. The problem I am having is that the user is prompted to enter the value twice (i.e. asks to enter A, B or X. User enters value then program asks again to enter A,B or X without performing the correct operation). Any help to have it operate after being entered would be great!
class output
{
static void Main(string[] args)
{
char userInput;
char upper;
Accounts myAccounts = new Accounts();
myAccounts.input();
Console.WriteLine("Enter an A to search account numbers");
Console.WriteLine("Enter a B to average the accounts");
Console.WriteLine("Enter X to quit the program");
Console.WriteLine("Enter an option --->");
userInput = Convert.ToChar(Console.ReadLine());
upper = char.ToUpper(userInput);
while (upper != 'X')
{
if (upper == 'A')
{
myAccounts.search();
Console.WriteLine("Enter an A to search account numbers");
Console.WriteLine("Enter a B to average the accounts");
Console.WriteLine("Enter X to quit the program");
Console.WriteLine("Enter an option --->");
userInput = Convert.ToChar(Console.ReadLine());
upper = char.ToUpper(userInput);
}
else if (upper == 'B')
{
myAccounts.average();
Console.WriteLine("Enter an A to search account numbers");
Console.WriteLine("Enter a B to average the accounts");
Console.WriteLine("Enter X to quit the program");
Console.WriteLine("Enter an option --->");
userInput = Convert.ToChar(Console.ReadLine());
upper = char.ToUpper(userInput);
}
else
Console.WriteLine("You entered an incorrect option, please select new option");
Console.WriteLine("Enter an A to search account numbers");
Console.WriteLine("Enter a B to average the accounts");
Console.WriteLine("Enter X to quit the program");
Console.WriteLine("Enter an option --->");
userInput = Convert.ToChar(Console.ReadLine());
upper = char.ToUpper(userInput);
}
}
}
推荐答案
尝试像这样重新格式化你的主文件:
Try reformatting your main like this:
static void Main(string[] args)
{
char userInput;
char upper;
Accounts myAccounts = new Accounts();
myAccounts.input();
do
{
Console.WriteLine("Enter an A to search account numbers");
Console.WriteLine("Enter a B to average the accounts");
Console.WriteLine("Enter X to quit the program");
Console.WriteLine("Enter an option --->");
userInput = Convert.ToChar(Console.ReadLine());
upper = char.ToUpper(userInput);
if (upper == 'A')
{
myAccounts.search();
}
else if (upper == 'B')
{
myAccounts.average();
}
else
Console.WriteLine("You entered an incorrect option, please select new option");
}
while (upper != 'X')
}
它并不完美,但它是一个很好的起点.基本上为什么您看到两次提示是因为您在进入 while 循环之前显示了一次菜单,然后在执行操作后再次显示.我提供的 do while 循环应该可以解决您的问题.
Its not perfect, but is a good starting point. Basically why you are seeing your prompt twice is because you show the menu once before entering your while loop, then again after executing the operation. the do while loop I have provide should fix your problem.
这篇关于while 循环重复用户输入 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!