我正在制作一个控制台应用程序,我有一个while循环在计算薪水时重新启动整个程序,另一个循环重新询问有关用户在输入错误时收到的薪水类型的问题。但是当我输入错误的选项时,会再次询问问题,整个程序也会重新启动。有没有办法让第二个while循环与第一个while循环分开进行反应?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WageCalc
{
class Program
{
static void Main(string[] args)
{
//Variables
string hourWorkedInput;
string wageInput;
double hourlyWage = 0.00;
int hoursWorkedAWeek = 0;
double payCheck;
bool wageCalculated = false;
bool AskQuestion = true;
//Actual Code
while (wageCalculated == false)
{
Console.WriteLine("Welcome to the Monthly Wage Calculator");
Console.WriteLine("Please enter how much you are paid an hour");
wageInput = Console.ReadLine();
hourlyWage = Double.Parse(wageInput);
Console.WriteLine("Please enter how many hours you work a week");
hourWorkedInput = Console.ReadLine();
hoursWorkedAWeek = Int32.Parse(hourWorkedInput);
Console.WriteLine("Do you get paid (W)eekly or (B)iweekly?");
string often = Console.ReadLine();
//Repeats if input is incorrect
while(AskQuestion)
{
if (often == "W")
{
payCheck = hoursWorkedAWeek * hourlyWage;
Console.WriteLine("You will make {0} on every paycheck", payCheck);
Console.WriteLine("Would you like to Calculate another Paycheck?");
string repeat = Console.ReadLine();
switch (repeat)
{
case "Y":
wageCalculated = false;
break;
case "N":
wageCalculated = true;
break;
default:
wageCalculated = true;
break;
}
}
else if (often == "B")
{
payCheck = (hoursWorkedAWeek * 2) * hourlyWage;
Console.WriteLine("You will make {0} on every paycheck", payCheck);
Console.WriteLine("Would you like to Calculate another Paycheck?");
string repeat = Console.ReadLine();
switch (repeat)
{
case "Y":
wageCalculated = false;
break;
case "N":
wageCalculated = true;
break;
default:
wageCalculated = true;
break;
}
}
else
{
Console.WriteLine("Please pick W or B");
AskQuestion = false;
}
}
}
}
}
}
答案 0 :(得分:2)
或者你可以让它变得更有趣和简单
private static void Main(string[] args)
{
//Actual Code
while (true)
{
Console.WriteLine("Welcome to the Monthly Wage Calculator");
Console.WriteLine("Please enter how much you are paid an hour");
double hourlyWage;
while (!double.TryParse(Console.ReadLine(), out hourlyWage))
Console.WriteLine("You had one job, now answer the question properly");
Console.WriteLine("Please enter how many hours you work a week");
double hoursWorkedAWeek;
while (!double.TryParse(Console.ReadLine(), out hoursWorkedAWeek))
Console.WriteLine("You had one job, now answer the question properly");
Console.WriteLine("Do you get paid (W)eekly or (B)iweekly?");
var often = Console.ReadLine();
while (often != "W" && often != "B")
{
Console.WriteLine("You had one job, now answer the question properly");
often = Console.ReadLine();
}
if (often == "W")
Console.WriteLine($"You will make {hoursWorkedAWeek * hourlyWage} on every paycheck");
else if (often == "B")
Console.WriteLine($"You will make {hoursWorkedAWeek * 2 * hourlyWage} on every paycheck");
Console.WriteLine("Would you like to play again Y/N?");
var repeat = Console.ReadLine();
while (repeat != "Y" && repeat != "N")
{
Console.WriteLine("You had one job, now answer the question properly");
repeat = Console.ReadLine();
}
if (repeat == "N")
{
Console.WriteLine("Suit yourself, goodbye");
break;
}
}
}