2014-11-08 145 views
0

我在Java中返回String類型的結果時出現問題。 這裏是整個代碼返回String類型的結果(Java)

import java.util.*; 

public class Multiplication 
{ 
    static Random randomNumbers = new Random(); 
    static Scanner input = new Scanner(System.in); 
    static int answer; 

public static void multiplication() 
{ 
    createQuestion(); //display first question 
    int guess; //student's answer 
    System.out.print("Your answer is (-1 to quite): "); 
    guess = input.nextInt(); 

    while(guess != -1) 
    { 
     checkAnswer(guess); 

     System.out.print("Your answer is (-1 to quite): "); 
     guess = input.nextInt(); 
    } 
} //end method multiplication 

//create new question 
public static void createQuestion() 
{ 
    int num_1 = randomNumbers.nextInt(10); 
    int num_2 = randomNumbers.nextInt(10); 
    answer = num_1 * num_2; 
    System.out.printf("How much is %d times %d?\n", num_1, num_2); 
}//end method createQuestion 

public static String createResponse(boolean correct) 
{ 
    if (correct) 

     switch(randomNumbers.nextInt(4)) 
     { 
     case 0: 
      return ("Very good!"); 
     case 1: 
      return("Excellent!"); 
     case 2: 
      return("Nice work!"); 
     case 3: 
      return ("Keep the good work"); 
     } //end switch 

//otherwise, assume incorrect 
     switch(randomNumbers.nextInt(4)) 
     { 
     case 0: 
      return("No. Please try again."); 
     case 1: 
      return("Wrong. Try once more."); 
     case 2: 
      return("Don't give up!"); 
     case 3: 
      return("No. Keep trying."); 
     }//end switch 

}//end method createResponse 

//check in the student answer correctly 
public static void checkAnswer(int guess) 
{ 
    if(guess != answer) 
    { 
     System.out.println(createResponse(false)); 
    } 
    else 
    { 
     System.out.print(createResponse(true)); 
     createQuestion(); 
    } 

}//end method checkAnswer 
}//end class Multiplication 

這裏是主要方法

public class MultiplicationTest { 

    public static void main(String[] args) { 

     Multiplication app = new Multiplication(); 
     app.multiplication(); 
    } 

}

的問題是在createResponse(boolean correct) method. Here JDE is saying that "This method must return a result of type String"。我在那裏提到了String類型的返回。但該程序沒有被執行。在createResponse方法下顯示一條紅線(布爾正確)。

有沒有人在哪裏我搞砸了? 在此先感謝!

+0

謝謝大家。我通過您的建議解決了我的問題。 對不起,延遲迴復。 – Mamun 2014-11-09 21:08:46

回答

2

編譯器無法斷言你的方法返回一個String。

這是因爲您的開關盒可能無法返回任何內容。

您可以通過放置

return null; 

在你的方法結束滿足編譯器。

+0

如果你感興趣爲什麼編譯器本質上不能檢測代碼是以哪種方式運行,請查看http://en.wikipedia.org/wiki/Halting_problem,但這遠遠超出了你問。 – 2014-11-08 18:54:47

1

您缺少createResponse方法中'switch-case'的'if'和'default'部分的'else'部分。

編輯: 好吧,'其他'是沒有必要的,但我錯過了那首先。第二個「開關」的縮進令人困惑。請使用括號來避免這種情況。

此外,編譯器認爲可能會發生這樣的情況,即'case'分支都不會被執行,因爲它不知道返回整數的範圍是0..3。您需要添加default個案來滿足編譯器。

1

方法createResponse在代碼中可能不總是返回聲明。如果第二個switch語句中沒有任何情況適用,它將在不返回的情況下到達代碼塊的底部。

只要確保你在方法的最後返回的東西(或找到另一個很好的解決方案):

return ""; 
}//end method createResponse 
1

這是因爲如果沒有條件無論是在switch語句中得到滿足就什麼也沒有回來。嘗試是這樣的:

public static String createResponse(boolean correct) 
{ 
    String result = null; 
    if (correct) 

     switch(randomNumbers.nextInt(4)) 
     { 
     case 0: 
      result = "Very good!"; 
// Insert the rest of the code here, assigning to result rather than returning as above. 
    return result; 
} 
1

編譯器是不夠聰明尚未知道nextInt(4)可能只返回0123則認爲,對於情況類似5你當前的代碼將不會返回任何東西,但如果方法聲明它會返回一些值,必須保證總會返回一些值。

要解決此問題,您可以將case 3:更改爲default:。這會使編譯器假定即使對於不是0,1,2的情況,也會返回一些值。

而且似乎你的代碼將是更清潔,如果你會使用else和額外的大括號一樣

public static String createResponse(boolean correct) { 
    if (correct){ 
     switch (randomNumbers.nextInt(4)) { 
     case 0: 
      return ("Very good!"); 
     case 1: 
      return ("Excellent!"); 
     case 2: 
      return ("Nice work!"); 
     default: 
      return ("Keep the good work"); 
     } 
    } else { 
     switch (randomNumbers.nextInt(4)) { 
     case 0: 
      return ("No. Please try again."); 
     case 1: 
      return ("Wrong. Try once more."); 
     case 2: 
      return ("Don't give up!"); 
     default: 
      return ("No. Keep trying."); 
     } 
    }// end switch 

}// end method createResponse 

BTW,你可以通過使用將存儲你的反應陣列簡化代碼一點。這樣,你的代碼可能看起來像

private static String[] good = { "Very good!", 
           "Excellent!", 
           "Nice work!", 
           "Keep the good work" }; 
private static String[] bad = { "No. Please try again.", 
           "Wrong. Try once more.", 
           "Don't give up!", 
           "No. Keep trying." }; 

public static String createResponse(boolean correct) { 
    if (correct) 
     return good[randomNumbers.nextInt(4)]; 
    else 
     return bad[randomNumbers.nextInt(4)]; 
} 

甚至

public static String createResponse(boolean correct) { 
    return correct ? good[randomNumbers.nextInt(4)] 
        : bad[randomNumbers.nextInt(4)]; 
} 
+0

我仍在學習使用數組。所以我希望我會在學習Array章節後再使用你的方法:) – Mamun 2014-11-09 21:11:51