![](/img/trans.png)
[英]@ParameterizedTest can't be resolved in IntelliJ IDEA 2017.3
[英]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.