亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java 重復的局部變量。難倒

Java 重復的局部變量。難倒

至尊寶的傳說 2022-08-03 15:51:42
我對編碼是全新的,并試圖讓我的第二個程序工作。它的作用非常簡單,但它在第24行“重復的局部變量確認”上拋出了一個錯誤。不能完全弄清楚為什么它不喜歡我正在做的事情。Scanner userInput = new Scanner(System.in);  char confirm;do{System.out.println("Welcome to the story teller");System.out.println("What is your name?");String name = userInput.nextLine();System.out.println("How old are you?");int age = userInput.nextInt();System.out.println("What country would you like to visit?");String country = userInput.nextLine();System.out.println("Great! So your name is" + name + ", you are" + age + "years old and you would like to visit" + country + "?");System.out.println("Press Y to continue or N to start over");char confirm = userInput.next().charAt(0);  if (confirm !='y' || confirm !='n'){ System.out.println("Sorry that input is not valid, please try again"); }  else {  System.out.println(name + "landed in" + country + "at the age of" + age + ".");  }} while(confirm == 'Y'|| confirm == 'y');
查看完整描述

4 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

因為您的“確認”變量已在作用域(第二行)中定義。如果要賦值,只需編寫confirm = userInput.next().charAt(0);



查看完整回答
反對 回復 2022-08-03
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

你聲明了兩次。將第二個聲明更改為僅分配給它,您應該沒問題:confirm

confirm = userInput.next().charAt(0);
// No datatype, so you aren't declaring confirm, just assigning to it


查看完整回答
反對 回復 2022-08-03
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

似乎除了重新聲明變量之外,還有一個或多個問題 -confirm

問題 1:

后。它不會提示輸入國家/地區,而是提示 。int age = userInput.nextInt()Press Y to continue or N to start over

此問題的原因:

由于您使用的是掃描儀,因此只會從輸入中獲取整數值,并且將跳過換行符。int age = userInput.nextInt();\n

修復

作為解決方法,我在之后添加了這樣,它將在之后消耗字符。userInput.nextLine();int age = userInput.nextInt();\nnextInt()

問題 2:

在第 1 次迭代后,此行將導致問題。confirm = userInput.next().charAt(0);

此問題的原因:

在第 2 次迭代中,您不會收到輸入名稱的提示,因為該行將采用上次迭代作為輸入,并將跳過并提示 age 。String name = userInput.nextLine();\nHow old are you?

修復

作為一種解決方法,我在之后添加了這樣一個,它將在之后使用字符,并且下一次迭代將按預期進行。userInput.nextLine();confirm = userInput.next().charAt(0);\nuserInput.next().charAt(0)

問題 3:

這個邏輯只期望和在,但在這里你是期望和兩者。if (confirm !='y' || confirm !='n')ynlowercasewhile(confirm == 'Y'|| confirm == 'y')yY

修復 - 我已經在下面的代碼中添加了必要的更改,但建議您將其更改為開關盒。

注意:

不建議在每次輸入后都這樣做,您可以簡單地解析它。有關詳細信息,請參閱此處。userInput.nextLine()

我不推薦它,但這會讓你的程序工作

Scanner userInput = new Scanner(System.in);

    char confirm;


    do {


        System.out.println("Welcome to the story teller");

        System.out.println("What is your name?");

        String name = userInput.nextLine();


        System.out.println("How old are you?");

        int age = userInput.nextInt();


        userInput.nextLine(); //adding this to retrieve the \n from nextint()


        System.out.println("What country would you like to visit?");

        String country = userInput.nextLine();


        System.out.println("Great! So your name is " + name + ", you are " + age

                + "years old and you would like to visit " + country + " ?");

        System.out.println("Press Y to continue or N to start over");

        confirm = userInput.next().charAt(0);


        userInput.nextLine(); //adding this to retrieve the \n this will help in next iteration


        System.out.println(name + " landed in " + country + " at the age of " + age + ".");


        if (confirm == 'y' || confirm == 'Y') {

            continue; // keep executing, won't break the loop

        } else if (confirm == 'n' || confirm == 'N') {

            break; // breaks the loop and program exits.

        } else {

            System.out.println("Sorry that input is not valid, please try again");

            // the program will exit

        }

    } while (confirm == 'Y' || confirm == 'y');

}

建議您使用 switch case 而不是 if 比較并解析字符和整數輸入并刪除任意添加作為解決方法。confirmationuserInput.nextLine()


查看完整回答
反對 回復 2022-08-03
?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

修復的另一個選項是刪除不必要的聲明char confirm;

并僅在需要時使用

char confirm = userInput.next().charAt(0);

根據@ScaryWombat建議,您需要更改變量的作用域(當前作用域與whiledo )


查看完整回答
反對 回復 2022-08-03
  • 4 回答
  • 0 關注
  • 242 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號