java while循环如何仅显示循环中的用户选择,而不是显示所有用户选项
您好,我正在处理这个菜单循环,但我对Java和eclipse还不熟悉,我有几个问题。我的主要问题是,当用户输入他们的选择时,程序不仅输出用户的选择,还输出用户选择之前菜单中的所有选择 例如,如果用户输入java while循环如何仅显示循环中的用户选择,而不是显示所有用户选项,java,loops,output,user-input,Java,Loops,Output,User Input,您好,我正在处理这个菜单循环,但我对Java和eclipse还不熟悉,我有几个问题。我的主要问题是,当用户输入他们的选择时,程序不仅输出用户的选择,还输出用户选择之前菜单中的所有选择 例如,如果用户输入g,程序将读取: you have selected to quit you have selected the coin toss simulator you have selected the grade estimator 有人知道我如何使它只输出用户的选择吗 此外,您是否建议我将
g
,程序将读取:
you have selected to quit
you have selected the coin toss simulator
you have selected the grade estimator
- 有人知道我如何使它只输出用户的选择吗李>
g
时,grade estimator
任何帮助都将不胜感激,我已经被困在这个问题上一段时间了
{
Scanner anotherScanner = new Scanner(System.in);
boolean usersSelection = false;
String c = "againAgain";
String upper = c.toUpperCase();
while (!usersSelection)
{
System.out.println("" + "Selection: ");
if (anotherScanner.hasNext("q"))
c = anotherScanner.next();
{
usersSelection = true;
System.out.println("you have selected to quit");
;
}
if (anotherScanner.hasNext("t"))
c = anotherScanner.next();
{
usersSelection = true;
System.out.println("you have selected the coin toss estimator");
;
}
if (anotherScanner.hasNext("g"))
{
{
c = anotherScanner.next();
{
usersSelection = true;
System.out.println("you have selected the grade estimator");
}
}
if (anotherScanner.hasNext("C"))
{
c = anotherScanner.next();
{usersSelection = true;}
System.out.print("You have selected color Challenge");
}
else
break;
}
// { { System.out.print("Selection");}
}
}}}
为
q
和t
条件括号更新if
,如下所示:
if (anotherScanner.hasNext("q"))
{
c = anotherScanner.next();
usersSelection = true;
System.out.println("you have selected to quit");
}
如果条件不包含括号,所有行都会因此执行我不是最棒的,但我很无聊,所以我要试一试! 首先,我建议你去买一些好书或者做一场马拉松式的辅导!有很多很好的视频教程:) 你这里的主要问题是范围。。问题是你的牙套。请看以下链接 设想一种情况,范围在
if(SomeBool)
1考虑以下代码:
{
...
if(SomeBool)
{
int x = 5;
x += 4;
System.out.println(x)
}
...
}
2-结果会和我写的一样
{
...
if(SomeBool)
{
int x = 5;
{
x += 4;
}
System.out.println(x)
}
...
}
{
...
if(SomeBool)
int x = 5;
x += 4;
...
}
3-但是如果我写了它就会崩溃
{
...
if(SomeBool)
{
int x = 5;
{
x += 4;
}
System.out.println(x)
}
...
}
{
...
if(SomeBool)
int x = 5;
x += 4;
...
}
在第一种情况下,所有代码都在同一范围内,因此可以访问x
如果两个x+=4
位于新的作用域级别,但x
包含在父作用域中,因此此新嵌套作用域可以访问它
如果三个x在不同范围内声明为x+=4代码>语句。如果您在If(SomeBool)
中省略括号,它将只执行下面的语句
案例三相当于:
{
...
if(SomeBool)
{ // Enter scope
int x = 5; // x created
} // Leave scope x deleted
x += 4; // There is no x here
System.out.println(x) // No x here either
...
}
当我们完成与我们的if(SomeBool)
关联的代码时,x
将被删除,因为我们不再在该范围内,它也不再被引用
因此,请尝试使用适当的缩进和大括号来清理代码。然后重新发布,因为它当前相当于:
{
Scanner anotherScanner = new Scanner(System.in);
boolean usersSelection = false;
String c = "againAgain";
String upper = c.toUpperCase();
while (!usersSelection)
{
System.out.println("" + "Selection: ");
if (anotherScanner.hasNext("q"))
{
c = anotherScanner.next();
}
usersSelection = true;
System.out.println("you have selected to quit");
if (anotherScanner.hasNext("t"))
{
c = anotherScanner.next();
}
usersSelection = true;
System.out.println("you have selected the coin toss estimator");
if (anotherScanner.hasNext("g"))
{
c = anotherScanner.next();
usersSelection = true;
System.out.println("you have selected the grade estimator");
if (anotherScanner.hasNext("C"))
{
c = anotherScanner.next();
usersSelection = true;
System.out.print("You have selected color Challenge");
}
else
{
break;
}
}
}
}
希望这能给我们带来一些启示 请用适当的缩进正确格式化代码。这对我们来说更容易阅读,甚至可能在过程中向您指出您的问题。一定要格式化你的if语句,比如if(){}
Wow,这是一个括号盛会!