为了账号安全,请及时绑定邮箱和手机立即绑定

在 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条经验 获得超7个赞

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 关注
  • 148 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信