在 Java 中用字符串重复整个循环很困难

我是编程新手,我开始练习 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!");


墨色风雨
浏览 149回答 3
3回答

largeQ

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

慕虎7371278

实现它的最简单方法是使用您的选择变量来控制循环条件: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!");

BIG阳

你只需要让循环重新开始        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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java