尝试使用带有 'menu' 的 For 循环
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] )
括号中的 none 是强制性的,因此我们也可以去掉 [ForInit]
和 [ForUpdate]
( 但保留分号 ).这具有不使用 [ForInit]
初始化任何东西并且在使用 [ForUpdate]
循环的每次迭代结束时不执行任何操作的效果,让我们只检查 [=20= 给出的退出条件] 表达式(当它的计算结果为 false
时,循环退出)。
请注意 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;
}
}
这里是初学者,请尽量解释清楚!
课程问题要求我创建菜单(已完成)。
菜单上的多个选项给出不同的一次性结果(完成)。
现在它要我实现 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] )
括号中的 none 是强制性的,因此我们也可以去掉 [ForInit]
和 [ForUpdate]
( 但保留分号 ).这具有不使用 [ForInit]
初始化任何东西并且在使用 [ForUpdate]
循环的每次迭代结束时不执行任何操作的效果,让我们只检查 [=20= 给出的退出条件] 表达式(当它的计算结果为 false
时,循环退出)。
请注意 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;
}
}