[英]Trying to use a For-Loop with a 'menu'
初學者在這里,請盡量說明!
一個課程問題要求我創建一個菜單(完成)。
菜單上有多個選項可提供不同的一次性結果(完成)。
現在它要我實現for
, while
和do...while
循環(不能理解)
我確實嘗試了所有基本知識,包括在for
循環內創建和填充數組(事后看來這是一個愚蠢的想法)。
public void displayMenu()
{
System.out.println("A. Option #A");
System.out.println("B. Option #B");
System.out.println("C. Option #C");
System.out.println("D. Option #D");
System.out.println("X. Exit!");
System.out.println();
System.out.println("Please enter your choice:");
}
public void start()
{
displayMenu();
Scanner console = new Scanner(System.in);
String input = console.nextLine().toUpperCase();
System.out.println();
switch (input)
{
case "A": System.out.println("Option #A was selected"); break;
case "B": System.out.println("Option #B was selected"); break;
case "C": System.out.println("Option #C was selected"); break;
case "D": System.out.println("Option #D was selected"); break;
case "X": System.out.println("You chose to Exit"); break;
default: System.out.println("Invalid selection made"); break;
}
}
public void startFor()
{
/*Each of these methods will modify the original start() method, each
*will add a loop of the specific type so that the menu is displayed
*repeatedly, until the last option is selected. When the last option
*is selected, exit the method (i.e. stop the loop).
*/
}
至於你問的一個示例for
在評論。
練習的重點似乎是在菜單上進行迭代,直到滿足退出條件為止( "X".equals(input)
)。 這意味着,除了在for
語句中的三個條件之間,這是您需要指定的唯一條件。 這是因為(基本) for
語句的一般形式是
for ( [ForInit] ; [Expression] ; [ForUpdate] )
在方括號之間的所有術語都不是強制性的情況下,因此我們也可以擺脫[ForInit]
和[ForUpdate]
( 但保留分號 )。 這還沒有初始化與任何的影響[ForInit]
並在與循環的每次迭代結束無所事事[ForUpdate]
留給我們只檢查由給定的退出條件[Expression]
表達式(當它的評估為false
,循環退出)。
注意, console
是在循環外部聲明的,因為在每次迭代中分配一個console
是很浪費的。 還要input
,因為您需要在for
語句的條件下使用它。
Scanner console = new Scanner(System.in);
String input = "";
for (;!"X".equals(input);) { // notice, the first and last part of the for loop are absent
displayMenu();
input = console.nextLine().toUpperCase();
System.out.println();
switch (input) {
case "A": System.out.println("Option #A was selected"); break;
case "B": System.out.println("Option #B was selected"); break;
case "C": System.out.println("Option #C was selected"); break;
case "D": System.out.println("Option #D was selected"); break;
case "X": System.out.println("You chose to Exit"); break;
default: System.out.println("Invalid selection made"); break;
}
}
您可能會注意到這有點尷尬,因為這通常不是for
循環的用途。
無論如何,在這一點上, while
版本變得很簡單( while (!"X".equals(input))
),在這種情況下, do...while
也等效,( do { ... } while (!"X".equals(input))
),因為相同的條件在當前循環的末尾和下一循環的開始均適用,並且它們之間沒有副作用。
順便說一句,您可能會注意到while (condition)
和for (; condition ;)
在功能上是等效的,並且您可能會迷惑為什么應該使用一個而不是另一個。 答案是可讀性。 當您在while (condition)
進行操作時,要做什么要清楚得多。
for循環中的所有參數不是強制性的。 定義一個停止標志並檢查輸入是否為“ X”。 每當輸入為“ X”時,只需更改stopFlag或簡單地使用break語句就可以中斷循環。
public void startFor()
{
boolean stopFlag = false;
for(; stopFlag == false ;) {
displayMenu();
Scanner console = new Scanner(System.in);
String input = console.nextLine().toUpperCase();
System.out.println();
switch (input)
{
case "A": System.out.println("Option #A was selected"); break;
case "B": System.out.println("Option #B was selected"); break;
case "C": System.out.println("Option #C was selected"); break;
case "D": System.out.println("Option #D was selected"); break;
case "X": System.out.println("You chose to Exit"); break;
default: System.out.println("Invalid selection made"); break;
}
if(input.contentEquals("X"))
stopFlag = true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.