小编典典

Java错误缺少返回陈述

java

好了,我正在尝试编写这段代码,但是我一直收到这个愚蠢的错误。我不知道我在做什么错,所以也许你们中的一位专家可以帮助我。

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." );

        }
    }
}

}


阅读 339

收藏
2020-11-30

共1个答案

小编典典

在方法promCommif 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";
  }
2020-11-30