Java需要来自两个单独输入的哨兵值

时间:2019-10-19 21:00:33

标签: java java.util.scanner sentinel

我刚刚遇到了哨兵控制的迭代,但遇到了一些困难。我必须编写一个程序来获取用户使用的里程和加仑,并计算总mpg。我试图在用户完成操作时使用-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);
            }

    }
}

4 个答案:

答案 0 :(得分:0)

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;
}

答案 1 :(得分:0)

只需在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);
        }

}
}

答案 2 :(得分:0)

您需要检查用户是否在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");
}

答案 3 :(得分:0)

谢谢大家的帮助。这是完成的工作代码:

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);
    }


}