好了,我正在尝试编写这段代码,但是我一直收到这个愚蠢的错误。我不知道我在做什么错,所以也许你们中的一位专家可以帮助我。
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." );
}
}
}
}
最佳答案
在方法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";
}