c#程序不断询问用户输入问题不止一次
本文关键字:用户 输入 问题 不止一次 程序 | 更新日期: 2023-09-27 18:06:35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
enter code here`namespace fuel_consumption {
class Program {
// Program Reset & exit method at the end of program
static bool DoItAgain()
{
bool startAgain = true;
string reply = "";
Console.Write("Start Over? (Y or N): ");
reply = Console.ReadLine();
reply = reply.ToUpper();
if (reply != "Y")
{
startAgain = false;
}
return startAgain;
}//End DoItAgain method
//Startup Screen method
static void WelcomeMessage() {
Console.WriteLine("'n'n'tWelcome to the Fuel Consumption Calculator'n'n't");
}// End startup Screen method
//Begin user input method for Number of Litres
static int InputLitres() {
string userInput = "";
int selection = 0;
int minLitres = 20;
bool inValid = true;
//User Input Message
while (inValid) {
Console.Write("'nEnter the amount of litres consumed: ");
程序一遍又一遍地问这个问题,实际上大约问了5次。
userInput = Console.ReadLine();
if (int.TryParse(userInput, out selection))
if (selection < minLitres) {
// Deliver Error Message to User
Console.Write("'nPlease Enter an amount 20 litres or above'n'n Please Try Again:'n");
}
else {
inValid = false;
}
}
//return the value entered by the user
return selection;
}//end InputLitres
//Begin InputKM method
static int InputKM() {
//set user input varibles
string userInput = "";
int selection = 0;
int inputLitres = InputLitres();
int minKms = 8 * inputLitres;
bool inValid = true;
while (inValid) {
Console.Write("'nEnter Kilometres Travelled: ");
然后它也会问这个问题几次
userInput = Console.ReadLine();
if (int.TryParse(userInput, out selection))
if (selection < minKms) {
//Deliver Error Message to user and redirect back to user input of kms
Console.WriteLine("'n Minimum Kms is {0:f2} Kilometres, Please Enter a value of {0:f2} or higher", minKms);
}
else {
inValid = false;
}
}
//return the KM Value
return selection;
}//End Input Kms
static double consumptionCalculation() {
int litres;
int kms;
double litresFormula;
double formulaResult;
//Define Base Varibles
formulaResult = 0.0;
litresFormula = 0.0;
litres = InputLitres();
kms = InputKM();
//Calculate fuel consumption in litres per 100km
litresFormula = (double)litres * 100;
formulaResult = (double)litresFormula / kms;
{
//Return the result value
return formulaResult;
}
}
//Print results method
static void PrintResults() {
double kmResult = consumptionCalculation();
Console.WriteLine("'n'n'tYour Fuel Consumption is {0} Litres per 100 Kilometres", kmResult);
}
//Start Program Loop Method
static void ProgramLoop() {
bool startAgain = true;
//Loop through each user Input Method
InputLitres();
InputKM();
consumptionCalculation();
PrintResults();
startAgain = DoItAgain();
}
static void Main(string[] args) {
WelcomeMessage();
ProgramLoop();
}
}
}
谁能给我一个主意,我哪里错了?我只需要它问一次这些问题,返回值。请让它简单,因为我是新的。由于
这就是你需要做的事情。
void Main()
{
WelcomeMessage();
ProgramLoop();
}
static void ProgramLoop()
{
bool startAgain = true;
while (startAgain)
{
int litres = InputLitres();
int kms = InputKM(litres);
double consumption = consumptionCalculation(litres, kms);
PrintResults(consumption);
startAgain = DoItAgain();
}
}
static bool DoItAgain()
{
Console.Write("Start Over? (Y or N): ");
string reply = Console.ReadLine();
reply = reply.ToUpper();
return reply.ToUpper() == "Y";
}
static void WelcomeMessage()
{
Console.WriteLine("'n'n'tWelcome to the Fuel Consumption Calculator'n'n't");
}
static int InputLitres()
{
int selection = -1;
int minLitres = 20;
bool invalid = true;
while (invalid)
{
Console.Write("'nEnter the amount of litres consumed: ");
if (int.TryParse(Console.ReadLine(), out selection))
{
invalid = selection < minLitres;
if (invalid)
{
Console.Write("'nPlease Enter an amount 20 litres or above'n'n Please Try Again:'n");
}
}
}
return selection;
}
static int InputKM(int litres)
{
int selection = -1;
int minKms = 8 * litres;
bool invalid = true;
while (invalid)
{
Console.Write("'nEnter Kilometres Travelled: ");
if (int.TryParse(Console.ReadLine(), out selection))
{
invalid = selection < minKms;
if (invalid)
{
Console.WriteLine("'nMinimum Kms is {0:f2} Kilometres, Please Enter a value of {0:f2} or higher", minKms);
}
}
}
return selection;
}
static double consumptionCalculation(int litres, int kms)
{
return (double)litres * 100.0 / (double)kms;
}
static void PrintResults(double consumption)
{
Console.WriteLine("'n'n'tYour Fuel Consumption is {0} Litres per 100 Kilometres", consumption);
}