import java.util.Scanner;
public class Rainfall{
public static void main(String[] args){
Scanner keyboard= new Scanner(System.in);
double total;
int[] numbers= new int[12];
total= userAnswere(numbers);
}//
public static double userAnswere(int[] number){
Scanner keyboard= new Scanner(System.in);
double userMonth;
for(int i = 1; i < number.length; i++){
System.out.println("Please enter a number for month "+i+" rainfall: ");
userMonth= keyboard.nextDouble();
userMonth+=number[i];
}
return userMonth;
}
}
答案 0 :(得分:0)
您有一条路径,在返回时无法初始化userMonth。 (尽管您和我可以看到,编译器将检查限制在方法的范围内。)
所以我认为你想要完成的是“向用户询问所有12个月的降雨情况并将每个月的回复保存在'数字'中并返回总数。
所以你的'userAnswere'应该是这样的:
public static double userAnswere(int[] number){
Scanner keyboard= new Scanner(System.in);
double userMonth = 0;
for(int i = 0; i < number.length; i++){
// assume user doesn't speak 0-based
System.out.println("Please enter a number for month "+(i+1)+" rainfall: ");
number[i] = keyboard.nextDouble();
userMonth += number[i];
}
return userMonth;
}
另请注意,你有一个演员会产生不良结果 - 加倍到int - 所以总数,尽管它是一个双倍也将是一个整数。