第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為什么我不能在case語句java中返回值

為什么我不能在case語句java中返回值

躍然一笑 2021-09-12 10:29:31
我正在制作一個(gè)簡(jiǎn)單的程序,它使用1 到 10之間的隨機(jī)數(shù)生成一個(gè)隨機(jī)數(shù)學(xué)問題。運(yùn)算符也將在+,- 和 *之間隨機(jī)。當(dāng)我嘗試使用 case 語句并返回操作值并打印問題(最后)時(shí),它說沒有操作變量。    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();    
查看完整描述

3 回答

?
偶然的你

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊

您需要在 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 block

int 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();    


查看完整回答
反對(duì) 回復(fù) 2021-09-12
?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

在外部聲明參數(shù)并在 switch case 中設(shè)置。所以這段代碼會(huì)是這樣的;


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 value

String 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();


查看完整回答
反對(duì) 回復(fù) 2021-09-12
?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

問題是您沒有遵循變量可見性范圍。您需要計(jì)算括號(hào) {} 這是一個(gè)通用示例。


    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;

     }


    }


查看完整回答
反對(duì) 回復(fù) 2021-09-12
  • 3 回答
  • 0 關(guān)注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)