我无法显示“它不是一个完美的数字”。我需要最后一部分的帮助。它不允许我将其更改为其他。
package editmess;
import java.util.Scanner;
public class Editmess {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("\nPerfect Number Finder Program");
System.out.print("\nEnter the start value: ");
int starval = scanner.nextInt();
System.out.print("Enter the end value:");
int endval = scanner.nextInt();
for (int n1 = starval; n1 < endval; n1++) {
int sum = 0;
for (int n2 = 1; n2 < n1; n2++) {
if (n1 % n2 == 0) {
sum = sum + n2;
}
}
if (sum == n1) {
System.out.println(n1 + " is a perfect number");
if (sum != n1) {
System.out.println("There is no perfect number between " + starval + " and " + endval);
break;
}
}
}
}
}
答案 0 :(得分:0)
除非在那里有另一个if语句,否则不能在else语句和if语句中添加else语句。
这是整个代码。你也没有阅读所有输入的数字。第一个for循环应该是n1&lt; = endval;而不是n1&lt; endval;这样它也会检查输入的enval。
Scanner scanner = new Scanner(System.in);
int counter = 0;
System.out.println("\nPerfect Number Finder Program");
System.out.print("\nEnter the start value: ");
int starval = scanner.nextInt();
System.out.print("Enter the end value:");
int endval = scanner.nextInt();
for (int n1 = starval; n1 <= endval; n1++) {
int sum = 0;
for (int n2 = 1; n2 < n1; n2++) {
if (n1 % n2 == 0) {
sum = sum + n2;
}
}
if (sum == n1) {
System.out.println(n1 + " is a perfect number");
counter ++; //This will add one to the counter if this loop is enterd
}
if(n1 == endval){
System.out.println("FINISHED!");
break;
}
}
//If the counter is 0 then it will display the message
if(counter == 0){
System.out.println("THERE IS NO PERFECT NUMBERS");
}
}