我是编程新手,我开始练习 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!");
largeQ
慕虎7371278
BIG阳
相关分类