簡體   English   中英

Java詢問用戶是否要繼續程序

[英]Java ask user if they want to continue a program

您好,我很好奇是否有任何其他方法可以縮短此代碼,但在“輸入 1 繼續”中滿足輸入時它會循環程序所以程序會運行得更快

    System.out.print("Enter first number: ");
    fnum = sc.nextInt();
    System.out.print("Enter second number: ");
    snum = sc.nextInt();
    
    if (fnum <= snum) {
        while (fnum <= snum) {
            System.out.println(fnum);
            fnum++;
        }
    } else {
        while (fnum >= snum) {
            System.out.println(fnum);
            fnum--;
        }
    }
    do {
    System.out.print("Enter 1 to continute, enter any number to end: ");
    enter = sc.nextInt();
    if (enter == 1) {
        System.out.print("Enter first number: ");
        fnum = sc.nextInt();
        System.out.print("Enter second number: ");
        snum = sc.nextInt();
    
        if (fnum <= snum) {
            while (fnum <= snum) {
                System.out.println(fnum);
                fnum++;
            }
        } else {
            while (fnum >= snum) {
                System.out.println(fnum);
                fnum--;
            }
        }
      } else {
        System.out.println("************************************************");
        System.out.println("************************************************");
        System.out.println("******************Thank you*********************");
        System.out.println("************************************************");
        System.out.println("************************************************");
        System.exit(0);
    }
   } while (enter == 1);

} }

    int enter=0;
        do {
            if(enter==0){
                System.out.print("Enter first number: ");
                int fnum = sc.nextInt();
                System.out.print("Enter second number: ");
                int snum = sc.nextInt();
                if (fnum <= snum) {
                    while (fnum <= snum) {
                        System.out.println(fnum);
                        fnum++;
                    }
                } else {
                    while (fnum > snum) {
                        System.out.println(fnum);
                        fnum--;
                    }
                }
            }
            System.out.print("Enter 0 to continue, enter any number to end: ");
            enter = sc.nextInt();

        } while (enter==0);

試試這種方式,你的重復代碼可以被刪除。

因此,每當您嘗試根據用戶輸入滿足的條件運行代碼時,請確保始終在 while 循環內終止程序,因為do whilewhile主要用於無限期執行代碼的目的。直到滿足某個條件。 就您而言,由於您必須至少執行一次代碼,因此 do while 是您程序的完美選擇

第一步是考慮你想重復什么。 無論用戶想要做什么,您都希望至少執行一次循環,因此do-while是完美的。

用戶輸入前兩個值后,您希望提示用戶確定他們是否要繼續,這構成了退出條件的一部分

Scanner sc = new Scanner(System.in);
String response = "Y";
do {
    System.out.print("Enter first number: ");
    int fnum = sc.nextInt();
    sc.nextLine();
    System.out.print("Enter second number: ");
    int snum = sc.nextInt();
    sc.nextLine();

    if (fnum <= snum) {
        while (fnum <= snum) {
            System.out.println(fnum);
            fnum++;
        }
    } else {
        while (fnum >= snum) {
            System.out.println(fnum);
            fnum--;
        }
    }

    System.out.println("Do you wish to try again [Y/N]?");
    response = sc.nextLine();
} while ("Y".equalsIgnoreCase(response));
System.out.println("************************************************");
System.out.println("************************************************");
System.out.println("******************Thank you*********************");
System.out.println("************************************************");
System.out.println("************************************************");

注意:出於教育原因,我使用response作為退出變量,您可以直接使用sc.nextLine()

因此,基本上,這將繼續循環,直到用戶在“繼續”問題中輸入Y任何內容(您也可以檢查N ,但這取決於您)

我只是這樣做

    do {
        // your code
        System.out.print("Press [1] to continue: ");
    } while (sc.nextInt() == 1);

把你的代碼放在里面,如果你用 do while 方法輸入 1,它會重復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM