方法getResponseToCorrectAnswer
和getResponseToIncorrectAnswer
不会返回任何字符串,但是打印将起作用,因此它可以使用该方法。有谁知道如何让这两种方法正确返回String
?
import java.util.Scanner;
public class CAI {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int answer;
for(int i = 1; i < 11; i++){
int num1 = generateRandomSingleDigitNumber();
int num2 = generateRandomSingleDigitNumber();
generateMultiplicationQuestion(num1, num2);
answer = input.nextInt();
validateMultiplicationAnswer(num1, num2, answer);
}//end for
}//end main
public static void generateMultiplicationQuestion(int num1, int num2){
System.out.println("What is the product of " + num1 + " x " + num2 + "?");
}//end generateMultiplicationQuestion
public static int generateRandomSingleDigitNumber(){
return (int)(Math.random()*10);
}//end generateRandomSingleDigitNumber
public static String getResponseToCorrectAnswer(){
//return ("Correct");
int num;
num = (int)(Math.random()*4);
if(num == 3){
return("Very good");
}else if(num == 2){
return("Excellent!");
}else if(num == 1){
return("Nice work!");
}else{
return("Keep up the good work!");
}//end if
}//end getResponseToCorrectAnswer
public static String getResponseToIncorrectAnswer(){
int num;
num = (int)(Math.random()*4);
if(num == 3){
return("No. Please try again.");
}else if(num == 2){
return("Wrong. Try once more");
}else if(num == 1){
return("Don't give up. Keep trying.");
}else{
return("Incorrect. Please answer again.");
}//end if
}//end getResponseToIncorrectAnswer
public static boolean validateMultiplicationAnswer(int num1, int num2, int answer) {
if(answer == num1 * num2){
getResponseToCorrectAnswer();
return true;
}else{
getResponseToIncorrectAnswer();
return false;
}//end if
}//end validateMultiplicationAnswer
}//end class
答案 0 :(得分:3)
方法getResponseToCorrectAnswer和getResponseToIncorrectAnswer不会返回任何字符串
当然可以,但你没有把字符串分配给任何东西,所以你看不到它。
试试这个
String response = getResponseToCorrectAnswer();
System.out.println(response);
您将看到该响应包含返回值。