第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

創(chuàng)建 while 和 for 循環(huán)以查看是否大于、等于或小于 B。然后打印結(jié)果

創(chuàng)建 while 和 for 循環(huán)以查看是否大于、等于或小于 B。然后打印結(jié)果

揚帆大魚 2023-06-14 14:31:28
我想使用 while 或/和 for 循環(huán)來比較 a 和 b 的值(由用戶輸入),然后打印哪個更大,如果其中一個等于零,則循環(huán)停止。我試圖為它編寫代碼但沒有運氣,因為我不理解 while 和 for 循環(huán)。import java.util.Scanner;public class Set31 {    public static void main(String args[])        {            int a = 1;            int b = 4;            Scanner input = new Scanner(System.in);            while (a < b && a!=0 && b!=0)            {                System.out.println("b is bigger than a");                System.out.print("enter a ");                a = input.nextInt();                System.out.print("enter b ");                b = input.nextInt();            }               while (a > b && a!=0 && b!=0)            {                System.out.println("a is bigger than b");                System.out.print("enter a ");                a = input.nextInt();                System.out.print("enter b ");                b = input.nextInt();        }            while (a == b && a!=0 && b!=0)            {                System.out.println("a is equal to b");                System.out.print("enter a ");                a = input.nextInt();                System.out.print("enter b ");                b = input.nextInt();            }    }}在我輸入輸入數(shù)字大約 1-4 次后運行程序后循環(huán)停止。
查看完整描述

3 回答

?
鳳凰求蠱

TA貢獻(xiàn)1825條經(jīng)驗 獲得超4個贊

您的代碼周圍沒有主循環(huán)。以下所有 while 循環(huán)都應(yīng)該可以解決問題。

while(a!=0 && b!=0) {
...
}

因此,如果您的輸入之一為零,則不會繼續(xù)循環(huán)并且例程結(jié)束。否則,您的其他三個循環(huán)之一將處理輸入,請求新輸入,然后由主循環(huán)重新開始。


查看完整回答
反對 回復(fù) 2023-06-14
?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗 獲得超13個贊

您可以為此使用一個do-while循環(huán),該循環(huán)將始終至少執(zhí)行一次,然后在bora等于 0 時退出:


public static void main(String[] args){


    @SuppressWarnings("resource")  //Used to remove unclosed warning

    Scanner input = new Scanner(System.in);


    int a;

    int b;


    do {

        System.out.println("Enter value for a: ");

        a = input.nextInt();

        System.out.println("Enter value for b: ");

        b = input.nextInt();


        if (a < b) {

            System.out.println("a is less than b");

        }

        else if (a > b) {

            System.out.println("a is greater than b");

        }

        else if (a == b) {

            System.out.println("a is equal to b");

        }

    } while (a != 0 && b != 0);


    System.out.println("Loop has finished!");

}

示例運行:


Enter value for a: 

1

Enter value for b: 

3

a is less than b

Enter value for a: 

3

Enter value for b: 

5

a is less than b

Enter value for a: 

0

Enter value for b: 

1

a is less than b

Loop has finished!

您不需要使用while循環(huán)來檢查每個條件,而是可以使用簡單的ifandif else語句來檢查每個外部循環(huán)的值是否更大。


查看完整回答
反對 回復(fù) 2023-06-14
?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗 獲得超10個贊

除了編輯循環(huán)條件之外,您還可以使用語句來改變控制流來退出循環(huán)中break。這可以這樣做:


int a;

int b;

Scanner input = new Scanner(System.in);

while(true) {

    System.out.println("Enter a: ");

    a = Integer.parseInt(input.nextLine());

    System.out.println("Enter b: ");

    b = Integer.parseInt(input.nextLine());

    if(a == 0 || b == 0) break;

    String comparison = (a > b) ? "is bigger than" : (a < b) ? "is less than" : "is equal to";

    System.out.println("a "+comparison+" b");

}

String exitReason = (a == 0 && b == 0) ? "a and b are" : (a == 0) ? "a is" : "b is";

System.out.println("Exited because "+exitReason+" equal to zero");


查看完整回答
反對 回復(fù) 2023-06-14
  • 3 回答
  • 0 關(guān)注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號