Java 需要来自两个独立输入的标记值
Java is requiring sentinel value from two separate inputs
我刚刚遇到哨兵控制的迭代,我遇到了一些困难。我必须制作一个程序,从用户那里获取使用的里程数和加仑数,计算总英里数。我试图在用户完成时使用 -1 作为标记值退出循环,但是当我输入该值时,Java 不会终止程序。相反,它要求加仑的另一个值。我如何让它接受来自第一个输入的标记值?
示例:
输入英里或 -1 退出
-1
输入加仑
-1
终止
import java.util.Scanner;
public class Activity3_17 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// processing phase
int miles = 1;
int gallons = 1;
int totalMiles = 0;
int totalGallons = 0;
float mpg = 0;
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
System.out.println("Enter gallons");
gallons = input.nextInt();
while (miles != -1) {
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
System.out.println("Enter gallons or -1 to exit");
gallons = input.nextInt();
totalMiles = totalMiles + miles;
totalGallons = totalGallons + gallons;
}
if (miles == -1) {
System.out.print("Terminate");
}
else{
mpg = (float) totalMiles / totalGallons;
System.out.println(mpg);
}
}
}
Java 仅在每次完成时评估 while
循环。因此,您需要手动检查 miles
是否为 -1 并打破循环。
while (miles != -1) {
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
if (miles == -1) break;
System.out.println("Enter gallons or -1 to exit");
gallons = input.nextInt();
totalMiles = totalMiles + miles;
totalGallons = totalGallons + gallons;
}
只需在 while 循环中添加 2 个附加条件(如果有 break)即可立即退出 while
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// processing phase
int miles = 1;
int gallons = 1;
int totalMiles = 0;
int totalGallons = 0;
float mpg = 0;
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
System.out.println("Enter gallons");
gallons = input.nextInt();
while (miles != -1) {
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
if(miles == -1) break;
System.out.println("Enter gallons or -1 to exit");
gallons = input.nextInt();
if(gallons == -1) break;
totalMiles = totalMiles + miles;
totalGallons = totalGallons + gallons;
}
if (miles == -1) {
System.out.print("Terminate");
}
else{
mpg = (float) totalMiles / totalGallons;
System.out.println(mpg);
}
}
}
您需要检查用户是否在 while
循环中输入了 -1。如果他们这样做了,请使用 break
退出循环,然后终止程序。
mpg
仍在循环中打印,但仅在进行检查之后。这确保了用户提供了有效的输入。
我决定将循环条件设为 true
,因为如果 miles
或 gallons
为 -1,循环就会中断。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int miles = 1;
int gallons = 1;
int totalMiles = 0;
int totalGallons = 0;
float mpg = 0;
while (true) {
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
if (miles == -1) break;
System.out.println("Enter gallons or -1 to exit");
gallons = input.nextInt();
if (gallons == -1) break;
totalMiles = totalMiles + miles;
totalGallons = totalGallons + gallons;
mpg = (float) totalMiles / totalGallons;
System.out.println(mpg);
}
input.close();
System.out.print("Terminate");
}
谢谢大家的帮助。这是完成的工作代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int miles = 0;
int gallons = 0;
int totalMiles = 0;
int totalGallon = 0;
double mpg = 0;
double totalMpg = 0;
while(miles != -1){
System.out.println("Enter mileage or -1 to exit: ");
miles = input.nextInt();
if(miles == -1){
break;
}
System.out.println("Enter gallons or -1 to exit: ");
gallons = input.nextInt();
mpg = (double) miles / gallons;
System.out.printf("MPG: %.4f%n", mpg);
if(gallons == -1){
break;
}
totalMiles = totalMiles + miles;
totalGallon = totalGallon + gallons;
}
if (miles == -1){
totalMpg = (double) totalMiles / totalGallon;
System.out.printf("Total used is %.4f%n", totalMpg);
}
}
我刚刚遇到哨兵控制的迭代,我遇到了一些困难。我必须制作一个程序,从用户那里获取使用的里程数和加仑数,计算总英里数。我试图在用户完成时使用 -1 作为标记值退出循环,但是当我输入该值时,Java 不会终止程序。相反,它要求加仑的另一个值。我如何让它接受来自第一个输入的标记值?
示例:
输入英里或 -1 退出
-1
输入加仑
-1
终止
import java.util.Scanner;
public class Activity3_17 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// processing phase
int miles = 1;
int gallons = 1;
int totalMiles = 0;
int totalGallons = 0;
float mpg = 0;
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
System.out.println("Enter gallons");
gallons = input.nextInt();
while (miles != -1) {
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
System.out.println("Enter gallons or -1 to exit");
gallons = input.nextInt();
totalMiles = totalMiles + miles;
totalGallons = totalGallons + gallons;
}
if (miles == -1) {
System.out.print("Terminate");
}
else{
mpg = (float) totalMiles / totalGallons;
System.out.println(mpg);
}
}
}
Java 仅在每次完成时评估 while
循环。因此,您需要手动检查 miles
是否为 -1 并打破循环。
while (miles != -1) {
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
if (miles == -1) break;
System.out.println("Enter gallons or -1 to exit");
gallons = input.nextInt();
totalMiles = totalMiles + miles;
totalGallons = totalGallons + gallons;
}
只需在 while 循环中添加 2 个附加条件(如果有 break)即可立即退出 while
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// processing phase
int miles = 1;
int gallons = 1;
int totalMiles = 0;
int totalGallons = 0;
float mpg = 0;
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
System.out.println("Enter gallons");
gallons = input.nextInt();
while (miles != -1) {
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
if(miles == -1) break;
System.out.println("Enter gallons or -1 to exit");
gallons = input.nextInt();
if(gallons == -1) break;
totalMiles = totalMiles + miles;
totalGallons = totalGallons + gallons;
}
if (miles == -1) {
System.out.print("Terminate");
}
else{
mpg = (float) totalMiles / totalGallons;
System.out.println(mpg);
}
}
}
您需要检查用户是否在 while
循环中输入了 -1。如果他们这样做了,请使用 break
退出循环,然后终止程序。
mpg
仍在循环中打印,但仅在进行检查之后。这确保了用户提供了有效的输入。
我决定将循环条件设为 true
,因为如果 miles
或 gallons
为 -1,循环就会中断。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int miles = 1;
int gallons = 1;
int totalMiles = 0;
int totalGallons = 0;
float mpg = 0;
while (true) {
System.out.println("Enter miles or -1 to exit");
miles = input.nextInt();
if (miles == -1) break;
System.out.println("Enter gallons or -1 to exit");
gallons = input.nextInt();
if (gallons == -1) break;
totalMiles = totalMiles + miles;
totalGallons = totalGallons + gallons;
mpg = (float) totalMiles / totalGallons;
System.out.println(mpg);
}
input.close();
System.out.print("Terminate");
}
谢谢大家的帮助。这是完成的工作代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int miles = 0;
int gallons = 0;
int totalMiles = 0;
int totalGallon = 0;
double mpg = 0;
double totalMpg = 0;
while(miles != -1){
System.out.println("Enter mileage or -1 to exit: ");
miles = input.nextInt();
if(miles == -1){
break;
}
System.out.println("Enter gallons or -1 to exit: ");
gallons = input.nextInt();
mpg = (double) miles / gallons;
System.out.printf("MPG: %.4f%n", mpg);
if(gallons == -1){
break;
}
totalMiles = totalMiles + miles;
totalGallon = totalGallon + gallons;
}
if (miles == -1){
totalMpg = (double) totalMiles / totalGallon;
System.out.printf("Total used is %.4f%n", totalMpg);
}
}