为什么我不能在case语句java中返回值

我正在制作一个简单的程序,它使用1 到 10之间的随机数生成一个随机数学问题。运算符也将在+,- 和 *之间随机。当我尝试使用 case 语句并返回操作值并打印问题(最后)时,它说没有操作变量。


    int number1 = (int)(Math.random()* 10) + 1;

    int number2 = (int)(Math.random()* 10) + 1;

    int operator = (int)(Math.random()* 3) + 1;



        switch (operator){

            case 1: {

                String operation = "+";

                int correctResult = number1 + number2;

                break;

            }

            case 2: {

                String operation = "-";

                int correctResult = number1 - number2;

                break;

            }

            case 3: {

                String operation = "*";

                int correctResult = number1 * number2;

                break;

            }

        }

    System.out.print(number1+operation+number2+": ");

    String studentAnswer = scanner.next();    


跃然一笑
浏览 162回答 3
3回答

偶然的你

您需要在 od switch 块之外声明操作:int number1 = (int)(Math.random()* 10) + 1;int number2 = (int)(Math.random()* 10) + 1;int operator = (int)(Math.random()* 3) + 1;String operation = null; // move outside of switch blockint correctResult; // move outside of switch block    switch (operator){        case 1: {            operation = "+";            correctResult = number1 + number2;            break;        }        case 2: {            operation = "-";            correctResult = number1 - number2;            break;        }        case 3: {            operation = "*";            correctResult = number1 * number2;            break;        }    }System.out.print(number1+operation+number2+": ");String studentAnswer = scanner.next();    

慕勒3428872

在外部声明参数并在 switch case 中设置。所以这段代码会是这样的;int number1 = (int) (Math.random() * 10) + 1;int number2 = (int) (Math.random() * 10) + 1;int operator = (int) (Math.random() * 3) + 1;//initalize value which is changing in swich case statement and set initializing valueString operation = "";int correctResult = 0;switch (operator) {    case 1: {        operation = "+";        correctResult = number1 + number2;        break;    }    case 2: {        operation = "-";        correctResult = number1 - number2;        break;    }    case 3: {        operation = "*";        correctResult = number1 * number2;        break;    }}System.out.print(number1 + operation + number2 + ": ");String studentAnswer = scanner.next();

慕丝7291255

问题是您没有遵循变量可见性范围。您需要计算括号 {} 这是一个通用示例。    public void exampleScopeMethod  {     String localVariable = "I am a local Variable";      {        String nextScope = " NextScope is One level deeper";        localVariable += nextScope      }      {         String anotherScope = "Is one level deeper than local variable, still different scope than nextScope";         //Ooops nextScope is no longer visible I can not do that!!!         anotherScope +=nextScope;       { one more scope , useless but valid }}      //Ooopppsss... this is again not valid      return nextScope;       // now it is valid       return localVariable;     }    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java