double average = calcAverage(grade1, grade2, grade3, grade4, grade5);
System.out.println("The average is: ");
determineGrade(average);
public static double calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5) {
double average = (grade1 + grade2 + grade3 + grade4 + grade5) / 5;
return average;
}
我一直收到determineGrade(average)
的错误,错误说明:
此方法必须返回double类型的结果
答案 0 :(得分:1)
您的所有输入都是整数。所以你的结果将是一个int。您需要将至少一个int转换为double,然后您的结果也将是双倍。
双倍平均值=(双倍)(1级+ 2级+ 3级+ 4级+ 5级)/ 5
答案 1 :(得分:1)
那是因为在determineGrade
你没有回报任何东西。你只打印一些东西,但什么也不返回。因此,请将determineGrade
的返回类型设为void,或者至少让它返回double
中的某些内容。
答案 2 :(得分:0)
Ok sorry, but the rest of the code comes as follows,
import java.util.*;
public class Stock
{
public static void main (String[] args)
{
int grade1=0;
int grade2=0;
int grade3=0;
int grade4=0;
int grade5=0;
// All of the below are gathering input from user
System.out.println("What is the first grade?");
Scanner input = new Scanner(System.in);
grade1=input.nextInt();
System.out.println("What is the second grade?");
Scanner input2 = new Scanner(System.in);
grade2=input.nextInt();
System.out.println("Whar is the third grade?");
Scanner input3 = new Scanner(System.in);
grade3=input.nextInt();
System.out.println("What is the fourth grade?");
Scanner input4 = new Scanner(System.in);
grade4=input.nextInt();
System.out.println("What is the fifth grade?");
Scanner input5 = new Scanner(System.in);
grade5=input.nextInt();
double average = calcAverage(grade1, grade2, grade3, grade4, grade5);
System.out.println("The average is: ");
determineGrade(average);
}
public static double calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5)
{
double average = (grade1 + grade2 + grade3 + grade4 + grade5) / 5;
return average;
}
public static double determineGrade(double average)
{
if (average>90)
{
System.out.println("You got an A");
}
else if (average>=80)
{
System.out.println("You got a B");
}
else if (average>=70)
{
System.out.println("You got a C");
}
else if (average>=60)
{
System.out.println("You got a D");
}
else if (average<60)
{
System.out.println("You got an F");
}
}
}