簡體   English   中英

知道為什么不能將 `ichoice` 解析為變量嗎?

[英]Any idea why `ichoice` can't be resolved to a variable?

我正在嘗試編寫一個 do while 循環來打印菜單,直到輸入選項 5。

我已經將我的變量ichoice聲明為一個整數,並且在我的最后一行代碼之前沒有遇到任何問題。 我花了一段時間才達到這一點,所以很困惑為什么它不起作用!

import java.util.Scanner;

public class testCoinSorter {
    
    public static void main(String[] args) {
        //An example
        
        CoinSorter exampleOne = new CoinSorter("Pound Sterling (£)", 0, 10000, new int[] {10,20,50,100,200});
        
    
        //System.out.println(1);
        do {
            System.out.println("Choice 1 - Coin Calculator");
            System.out.println("Choice 2 - Multiple coin calculator");
            System.out.println("Choice 3 - Print coin list");
            System.out.println("Choice 4 -- display program configurations");
            System.out.println("Choice 5 - quit the program & save the data");
            Scanner in = new Scanner(System.in);
            int ichoice;
            ichoice = in.nextInt();
            switch(ichoice) {
                case 1:
                          // put code for enrolling a new student here
                          System.out.println("Enrolling a new student");
                          // etc etc
                    break;
                case 2:
                    // put code for entering a students mark here
                    System.out.println("Enter the students mark");
                    // etc etc
                    break;
                case 3:
                    // put code for printing out results here
                    System.out.println("Printing out results");
                    // Etc etc
                    break;
                case 4:
                    // put code for calculating final grade here
                    System.out.println("printing final grade");
                    // etc etc
                    break;
                default:
                    if(ichoice != 5) System.out.println("Unknown option");
                    // no need for a break
            } // End of the switch statement
        } while (ichoice != 5);
        
        
    }
}

任何澄清將不勝感激

ichoice變量在do-while循環中定義。 這使得該變量僅在do-while循環塊范圍內可用(由{ }大括號定義)。

要使其在while ( )測試表達式中可訪問,您應該將ichoice移到循環體之外,例如在do之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM