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

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

在 Java 中用字符串重復整個循環很困難

在 Java 中用字符串重復整個循環很困難

墨色風雨 2021-11-03 16:14:14
我是編程新手,我開始練習 Java。在我的練習中,我要求編寫一個程序來計算并打印出一個數字的數字之和。然后它打印出和數的所有除數。問題是在那之后,我需要問用戶他們是否想嘗試另一個號碼,并且當這個人回答“是”時我無法重新啟動程序。謝謝你,對不起我的英語!//Introduction        System.out.println("\nWelcome to our Calculation Program!\n----------------------------------------");        System.out.print("Enter a number with at most 7-digits:");        int input = mykeyboard.nextInt();        int sum = 0;        while (input > 0) {            int add = input % 10;            sum = sum + add;            input = input / 10;        }        System.out.println("Sum of the digits of your input is: " + sum);        System.out.print("The divisors of " + sum + " are as follows: " );        for (int counter = 1; sum >= counter; counter++) {            if (sum % counter == 0)        System.out.print(counter + " ");    }         System.out.println("\n\nDo you want to try another number?");        Scanner mykeyboard2 = new Scanner(System.in);        String choice = mykeyboard2.nextLine();        if (choice.equals("yes")) {            System.out.println("Enter a number with a most 7-digits:");            while (input > 0);                int add = input % 10;                sum = sum + add;                input = input / 10;                System.out.println("Sum of the digits of your input is: " + sum);                System.out.print("The divisors of " + sum + " are as follows: " );                for (int counter = 1; sum >= counter; counter++)                    if (sum % counter == 0)                        System.out.print(counter + " ");} if (choice.equals("no")) {System.out.println("Thanks and Have a Great Day!");
查看完整描述

3 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

while (input > 0);

這將進入無限循環。我想你把一個;而不是{.


應該 while (input > 0){


編輯:


System.out.println("\nWelcome to our Calculation Program!\n----------------------------------------");


while(true){



    System.out.print("Enter a number with at most 7-digits:");

    int input = mykeyboard.nextInt();

    int sum = 0;



    while (input > 0) {

        int add = input % 10;

        sum = sum + add;

        input = input / 10;



    }

    System.out.println("Sum of the digits of your input is: " + sum);

    System.out.print("The divisors of " + sum + " are as follows: " );

    for (int counter = 1; sum >= counter; counter++) {

        if (sum % counter == 0)

        System.out.print(counter + " ");


    } 


    System.out.println("\n\nDo you want to try another number?");

    Scanner mykeyboard2 = new Scanner(System.in);

    String choice = mykeyboard2.nextLine();


    if (choice.equals("no")) {

        break;

    }

}


查看完整回答
反對 回復 2021-11-03
?
慕虎7371278

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

實現它的最簡單方法是使用您的選擇變量來控制循環條件:


System.out.println("\nWelcome to our Calculation Program!\n----------------------------------------");


String choice = "yes";

while(choice.equals("yes")) {

    System.out.print("Enter a number with at most 7-digits:");

    int input = mykeyboard.nextInt();

    int sum = 0;



    while (input > 0) {

        int add = input % 10;

        sum = sum + add;

        input = input / 10;



    }

    System.out.println("Sum of the digits of your input is: " + sum);

    System.out.print("The divisors of " + sum + " are as follows: " );

    for (int counter = 1; sum >= counter; counter++) {

        if (sum % counter == 0)

    System.out.print(counter + " ");


    System.out.println("\n\nDo you want to try another number?");

    Scanner mykeyboard2 = new Scanner(System.in);

    choice = mykeyboard2.nextLine();

}


System.out.println("Thanks and Have a Great Day!");


查看完整回答
反對 回復 2021-11-03
?
BIG陽

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

你只需要讓循環重新開始


        System.out.print("Enter a number with at most 7-digits:");

    int input = mykeyboard.nextInt();

    while(input != -1){

    int sum = 0;

        int add = input % 10;

        sum = sum + add;

        input = input / 10;




    System.out.println("Sum of the digits of your input is: " + sum);

    System.out.print("The divisors of " + sum + " are as follows: " );

    for (int counter = 1; sum >= counter; counter++) {

        if (sum % counter == 0)

    System.out.print(counter + " ");

    System.out.print("Do you want another number? If you dont type -1: ");

    input = mykeyboard.nextInt();

    }

代碼會一直運行直到用戶輸入 -1


查看完整回答
反對 回復 2021-11-03
  • 3 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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