private int repetition;
public int repetition(int sum){
Scanner sc = new Scanner(System.in);
System.out.println("Enter 5 numbers greater than 0: ");
for (int i = 0; i < number.length; i++){
System.out.println("Please enter number: ");
number[i] = sc.nextInt();
if (number[i] < 0){
System.out.println("Please enter a number greater than 0");
i--;
sum -= number[i];
}
sum += number[i];
}
return int sum;
}
repetition(sum);
}
我收到的错误是说我的方法声明无效,但我似乎无法弄清楚我做错了什么。
答案 0 :(得分:0)
请阅读以上评论
我修改了您的代码以使其有效,并添加了注释内联
// you need a `main` as the entry point to your program
public static void main(String... args) {
// my class is called `Main` so I instantiate it and call your method
System.out.println("the sum is " + new Main ().repetition());
}
// no parameters necessary
public int repetition(){
Scanner sc = new Scanner(System.in);
// keep a sum
int sum = 0;
System.out.println("Enter 5 numbers greater than 0: ");
for (int i = 0; i < 5; i++){ // loop five times
System.out.println("Please enter number: ");
// store into a local variable
int in = sc.nextInt();
// if less than zero then goto the top of the loop again
if (in < 0){
System.out.println("Please enter a number greater than 0");
i--;
continue;
}
// else add it to the sum
sum += in;
}
// finally return the sum
return sum;
}
答案 1 :(得分:-1)
倒数第二行。您的方法调用不在任何方法之内。