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

时间:2018-10-07 21:18:06

标签: 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 + " ");

}如果(choice.equals(“ no”)){

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您只需要做到这一点,即可重新开始循环

from fractions import Fraction

GPSDDLat = ( float(str(GPSLat.values[0])) 
           + float(str(GPSLat.values[1])) / 60
           + float(Fraction(str(GPSLat.values[2]))) / 3600
           )

代码将一直持续到用户键入-1

答案 2 :(得分:0)

最简单的方法是使用您的选择变量来控制循环条件:

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!");