2014-10-01 110 views
-3
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類型的結果

+3

對不起,我很困。你正在餵食你的int類型的方法參數,並期望它能推出一個double類型的方法參數。使用你的Google Fu並搜索「cast int double」,然後刪除這些問題。 – MarsAtomic 2014-10-01 23:25:53

+0

沒有足夠的代碼來說明發生了什麼。 'determineGrade()'的代碼在哪裏? – John3136 2014-10-01 23:27:52

+0

函數'determineGrade'在哪裏? – jww 2014-10-02 01:03:43

回答

1

所有的輸入都是整數。所以你的結果將是一個整數。你需要至少投一個int來加倍,那麼你的結果也是雙倍的。

雙平均=(雙)(grade1 + grade2 + grade3 + grade4 + grade5)/ 5

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"); 


     } 
    } 
    } 
+2

當您需要添加缺少的代碼時,請編輯原始問題。不要爲自己的問題添加答案,因爲這不是答案,是嗎?現在你是否搜索了「投入int加倍?」另外,請查看SO [幫助文件](http://stackoverflow.com/help)。它會幫助你使用這個網站來獲得更好的答案。 – MarsAtomic 2014-10-01 23:35:10

1

這是因爲在determineGrade你什麼都不返回。你只打印一些東西,但什麼都不返回。所以,請將退回類型determineGrade作廢,或者至少讓它返回double