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

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

如何用某個變量結束 while 循環

如何用某個變量結束 while 循環

溫溫醬 2023-07-13 14:35:43
我正在制作一個帶有 while 循環的奇數或偶數程序。我想弄清楚如何以某個數字結束 while 循環。現在我有 1 來繼續循環,并嘗試使 2 成為終止循環的數字。還試圖找出如果用戶輸入除數字(如字母/單詞)以外的任何內容時如何終止程序。package oddoreven;import java.util.Scanner;public class oddoreven {    public static void main (String[] args){                    int num;        int x = 1;        while(x == 1) {            System.out.println("Enter a number to check whether or not it is odd or even");            Scanner s = new Scanner(System.in);            num = s.nextInt();            if (num % 2 == 0)                System.out.println("The number is even");            else                 System.out.println("The number is odd");            //trying to figure out how to get the code to terminate if you put in a value that isn't a number            System.out.println("Type 1 to continue, 0 to terminate");            x = s.nextInt();        }    }}
查看完整描述

4 回答

?
明月笑刀無情

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

您應該嘗試使用“真正的終止條件”來終止循環while(或任何與此相關的循環);它更干凈,應該更容易被其他人理解。

在你的情況下,我認為最好有一個do-while循環,圍繞這個邏輯有一些條件:num % 2 == 0,以及一個用于處理用戶輸入/驗證的內部while循環。

查看完整回答
反對 回復 2023-07-13
?
繁星coding

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

我沒有完全遵循您想要的條件,因為除非有其他選項,否則 擁有繼續條件和終止條件是沒有意義的。

如果用戶輸入,您希望用戶做什么3,4或者5?退出代碼還是繼續代碼?好吧,如果默認是退出,那么您不需要退出代碼,2因為它已經退出了!如果默認是繼續,則不需要繼續1,只需要退出2。因此,在這種情況下兩者都做是沒有意義的。

以下是使用do while循環的修改后的代碼,以確保循環至少進入1 次:

   int x;

    do {

        System.out.println("Enter a number to check whether or not it is odd or even");

        Scanner s = new Scanner(System.in);

        int num = s.nextInt();

        if (num % 2 == 0)

            System.out.println("The number is even");

        else 

            System.out.println("The number is odd");

        //trying to figure out how to get the code to terminate if you put in a value that isn't a number

        System.out.println("Type 1 to check another number, anything else to terminate.");


        if (!s.hasNextInt()) {

            break;

        }

        else {

            x = s.nextInt();

        }

    } while(x == 1);

   }

請注意,我添加了一個檢查來!s.hasNextInt()檢查用戶是否輸入了除 an 之外的任何內容,并且在這些情況下int將通過從循環中終止而不拋出 an 來終止(這與在本例中終止程序相同)。Exceptionbreak


如果x是有效整數,則將x設為該值,然后循環條件檢查是否x為1。如果x不是1,則循環終止,如果是,則將再次繼續循環。


查看完整回答
反對 回復 2023-07-13
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

您可以嘗試的另一件事是,您可以繼續要求用戶輸入正確的輸入,并且只有在他們這樣做時才繼續操作,而不是退出程序。我不知道你的要求是什么,但如果你想遵循良好的代碼實踐,那么你不應該僅僅因為用戶輸入了錯誤的輸入而終止你的程序。想象一下,如果你用谷歌搜索一個有拼寫錯誤的單詞,然后谷歌就關閉了。


無論如何,這就是我的做法



import java.util.Scanner;


public class oddoreven {

    public static void main(String[] args) {

        int num;

        int x = 1;

        while (x == 1) {

            System.out.println("Enter a number to check whether or not it is odd or even");

            Scanner s = new Scanner(System.in);


            boolean isInt = s.hasNextInt(); // Check if input is int

            while (isInt == false) { // If it is not int

                s.nextLine(); // Discarding the line with wrong input

                System.out.print("Please Enter correct input: "); // Asking user again

                isInt = s.hasNextInt(); // If this is true it exits the loop otherwise it loops again

            }

            num = s.nextInt(); // If it is int. It reads the input


            if (num % 2 == 0)

                System.out.println("The number is even");

            else

                System.out.println("The number is odd");

            // trying to figure out how to get the code to terminate if you put in a value

            // that isn't a number

            System.out.println("Type 1 to continue, 0 to terminate");

            x = s.nextInt();

        }


    }

}


查看完整回答
反對 回復 2023-07-13
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

要在用戶輸入數字以外的任何內容時退出程序,請將變量 x 類型更改為字符串


if (!StringUtils.isNumeric(x)) {

    System.exit(0);

}

當用戶輸入 2 時退出程序


if (x == 2) {

    System.exit(0);

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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