你好,我正在处理这个菜单循环,但我是Java和eclipse的新手,我有一些问题。我的主要问题是,当用户输入他们的选择时,程序不仅输出用户的选择,而且在用户选择之前输出菜单中的所有选择。
例如,如果用户输入g
,程序将读取:
you have selected to quit
you have selected the coin toss simulator
you have selected the grade estimator
- 有人知道我如何才能让它只输出用户的选择吗
此外,您是否建议我在每次选择下嵌套此循环,并创建一个新的循环来运行用户的选择,例如当他们选择g
时的等级估计器?
任何帮助都将不胜感激,我已经被困了一段时间了!
{
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
条件不包含括号,因此所有行都执行
我不是最棒的,但我很无聊,所以我要试一试!首先,我建议你读一些好书,或者做一场马拉松式的教程!有很多好的视频教程:)
你的主要问题是范围。。问题出在你的牙套上。请查看以下链接!
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.htmlhttp://mindprod.com/jgloss/scope.html
想象一个范围在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)
}
...
}
3-然而,如果我写,它会崩溃
{
...
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;
}
}
}
}
希望这能带来一些启示!