【发布时间】:2014-02-11 18:22:52
【问题描述】:
您好,我正在处理这个菜单循环,但我是 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");}
}
}}}
【问题讨论】:
-
请使用适当的缩进正确格式化您的代码。这对我们来说更容易阅读,甚至可以在此过程中向您指出您的问题。请务必格式化您的 if 语句,例如
if () {} -
哇,这是一个支架盛宴!
标签: java loops output user-input