Java:如何在第一次输入后使用哨兵关闭循环(使用多个输入时)
Java: How to close loop with sentinel after first input (when using multiple inputs)
我花了几个小时试图解决这个问题,如果这是一个简单的解决方案,我深表歉意。
我想让这个程序更加人性化。目前一切正常,但哨兵要求我为行驶里程和使用的汽油输入“0”。我希望哨兵在我仅输入“0”英里后结束程序。这可能吗?
import java.util.Scanner;
public class Mileage
{
public static void main(String[] args)
{ //initialization
Scanner sc = new Scanner(System.in);
int miles = -1, gallons = -1,
totalMiles = 0, totalGallons = 0;
double average, runningAverage;
//while loop with sentinel controlled repetition
while (miles != 0 && gallons != 0)
{ //get miles
System.out.println("Enter the amount of miles driven. (0 to exit)");
miles = sc.nextInt();
//get gallons
System.out.println("Enter the amount of gallons used. (0 to exit)");
gallons = sc.nextInt();
//calc average
average = (double) miles / gallons;
//calc running average
totalMiles += miles;
totalGallons += gallons;
runningAverage = (double) totalMiles / totalGallons;
//output average only if values were entered, by using if statement
if (miles != 0 && gallons != 0)
{
System.out.printf("Miles per gallon for this trip is %.1f%n", average);
System.out.printf("Running average miles per gallon is %.1f%n", runningAverage);
}
}
}
}
我希望哨兵在我仅输入“0”英里后结束程序。
如果我明白你的要求,只需更改
while (miles != 0 && gallons != 0)
到
while (miles != 0)
和
System.out.println("Enter the amount of gallons used. (0 to exit)");
到
System.out.println("Enter the amount of gallons used.");
您可能会发现在 0
英里处终止循环更方便。喜欢,
miles = sc.nextInt();
if (miles == 0) {
break;
}
否则会再次提示gallons
当你这样做时:
miles = sc.nextInt();
您可以像这样检查退出程序:
if(miles==0){
System.exit(0);
}
就在之前:
System.out.println("Enter the amount of gallons used. (0 to exit)");
我花了几个小时试图解决这个问题,如果这是一个简单的解决方案,我深表歉意。
我想让这个程序更加人性化。目前一切正常,但哨兵要求我为行驶里程和使用的汽油输入“0”。我希望哨兵在我仅输入“0”英里后结束程序。这可能吗?
import java.util.Scanner;
public class Mileage
{
public static void main(String[] args)
{ //initialization
Scanner sc = new Scanner(System.in);
int miles = -1, gallons = -1,
totalMiles = 0, totalGallons = 0;
double average, runningAverage;
//while loop with sentinel controlled repetition
while (miles != 0 && gallons != 0)
{ //get miles
System.out.println("Enter the amount of miles driven. (0 to exit)");
miles = sc.nextInt();
//get gallons
System.out.println("Enter the amount of gallons used. (0 to exit)");
gallons = sc.nextInt();
//calc average
average = (double) miles / gallons;
//calc running average
totalMiles += miles;
totalGallons += gallons;
runningAverage = (double) totalMiles / totalGallons;
//output average only if values were entered, by using if statement
if (miles != 0 && gallons != 0)
{
System.out.printf("Miles per gallon for this trip is %.1f%n", average);
System.out.printf("Running average miles per gallon is %.1f%n", runningAverage);
}
}
}
}
我希望哨兵在我仅输入“0”英里后结束程序。
如果我明白你的要求,只需更改
while (miles != 0 && gallons != 0)
到
while (miles != 0)
和
System.out.println("Enter the amount of gallons used. (0 to exit)");
到
System.out.println("Enter the amount of gallons used.");
您可能会发现在 0
英里处终止循环更方便。喜欢,
miles = sc.nextInt();
if (miles == 0) {
break;
}
否则会再次提示gallons
当你这样做时:
miles = sc.nextInt();
您可以像这样检查退出程序:
if(miles==0){
System.exit(0);
}
就在之前:
System.out.println("Enter the amount of gallons used. (0 to exit)");