返回String类型的结果(Java)

时间:2014-11-08 18:38:31

标签: java string return

我在Java中返回String类型的结果时遇到问题。 这是整个代码

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方法下显示一条红线(布尔正确)。

有没有人搞砸了? 提前谢谢!

5 个答案:

答案 0 :(得分:2)

编译器无法断言您的方法返回String。

这是因为您的switch-case可能无法返回任何内容。

您可以通过放置

来满足编译器
return null;

在方法结束时。

答案 1 :(得分:1)

你错过了createResponse方法中'switch'和'default'的'switch'大小写的'else'部分。

修改: 好的,'其他'不是必需的,但我首先想念它。第二个'开关'的缩进令人困惑。请使用括号来避免这种情况。

此外,编译器认为可能会发生“case”分支都不会被执行,因为它不知道nextInt在0..3范围内返回整数。你需要添加default个案来满足编译器。

答案 2 :(得分:1)

方法createResponse可能无法始终在代码中返回return语句。如果第二个switch语句中没有一个案例适用,它将到达代码块的底部而不返回。

请确保在方法结束时返回一些内容(或找到另一个不错的解决方案):

return "";
}//end method createResponse

答案 3 :(得分: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;
}

答案 4 :(得分:1)

编译器还不够聪明,知道nextInt(4)可能只返回0123所以它假设5您当前的代码不会返回任何内容,但如果方法声明它将返回某些值,则必须保证始终返回某些值。

要解决此问题,您可以将case 3:更改为default:。这将使编译器假设即使对于不是012的情况,也会返回一些值。

如果您使用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

顺便说一句,你可以通过使用存储你的响应的数组来简化你的代码。这样你的代码就像

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)];
}