java—知道为什么“ichoice”不能解析为变量吗?

uurity8g  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(303)

我试图写一个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);

    }
}

如有任何澄清,将不胜感激

cxfofazt

cxfofazt1#

ichoice 变量是在 do-while 循环。这使得变量仅在 do-while 循环块范围(由
{ } 大括号)。
使其在 while ( ) 应该移动的测试表达式 ichoice 环体外部,例如 do .

相关问题