亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java 需要來自兩個單獨輸入的哨兵值

Java 需要來自兩個單獨輸入的哨兵值

Cats萌萌 2023-11-10 16:50:07
我剛剛遇到了哨兵控制迭代,并且遇到了一些困難。我必須編寫一個程序來獲取用戶使用的里程和加侖數,計算總英里數。我試圖使用 -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 回答

?
九州編程

TA貢獻1785條經驗 獲得超4個贊

您需要檢查用戶是否在while循環內輸入了-1。如果這樣做,請使用 退出循環break,然后終止程序。


mpg仍在循環內打印,但僅在進行檢查后打印。這確保用戶給出了有效的輸入。


我決定設置循環條件,因為如果ortrue為 -1 ,則循環應該中斷。miles gallons


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

}


查看完整回答
反對 回復 2023-11-10
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

只需在 while 循環中添加 2 個附加條件(如果帶有中斷)即可立即退出 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);

        }


}

}


查看完整回答
反對 回復 2023-11-10
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

這是完成的工作代碼:


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

    }



}


查看完整回答
反對 回復 2023-11-10
?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

whileJava 僅在每次完成時評估循環。因此,您需要手動檢查是否為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;

}


查看完整回答
反對 回復 2023-11-10
  • 4 回答
  • 0 關注
  • 223 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號