好了,我正在尝试编写这段代码,但是我一直收到这个愚蠢的错误。我不知道我在做什么错,所以也许你们中的一位专家可以帮助我。
import java.util.*; public class School { Random randQuest = new Random(); int userAnswer; public void createQuestion() { int range = 10; // range of numbers in question int num1 = randQuest.nextInt( range ); int num2 = randQuest.nextInt( range ); userAnswer = num1 * num2; System.out.printf( "How much is %d times %d?\n", num1, num2 ); } // prompt comment public String promComm( boolean answer ) { if ( answer ) { switch ( randQuest.nextInt( 1 ) ) { case 0: return( "Very Good!" ); } switch ( randQuest.nextInt( 1 ) ) { case 0: return( "No. Please try again." ); } } }
}
在方法promCommif answer中,false该方法不返回任何值。如果相同randQuest.nextInt(1) != 0.
promComm
answer
false
randQuest.nextInt(1) != 0.
应该是这样的:
public String promComm( boolean answer ){ if (answer){ switch (randQuest.nextInt(1)){ case 0: return("Very Good!"); } switch (randQuest.nextInt(1) ){ case 0: return( "No. Please try again." ); } return "Some value"; }else return "Some value"; }