【发布时间】:2014-01-19 02:41:08
【问题描述】:
每次我从选项列表中输入一个选项时,它只会告诉我再次输入一个选项,只有在用户输入不是选项的内容时才应该这样做。
对于 yes 和 no 选项,我在 while 循环中遇到了同样的问题,但在修复之后,我看不出它与除了更多选项之外我仍然遇到问题的区域之间有任何区别。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testing.tests;
import java.util.Scanner;
/**
*
* @author andyoppenheimer
*/
public class TestingTests {
static Scanner sc = new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String start_end;
String test_run = null;
int name_num = 0;
int LOOP_1 = 0;
int LOOP_2 = 0;
while (LOOP_1 == 0) {
System.out.println("Do you want to continue, yes or no?");
System.out.print("----->: ");
start_end = sc.next();
if ("yes".equals(start_end)) {
LOOP_1 = 1;
}
if ("no".equals(start_end)) {
LOOP_1 = 1;
System.exit(0);
}
if (!"no".equals(start_end) & !"yes".equals(start_end)) {
LOOP_1 = 0;
}
}
**
while (LOOP_2 == 0) {
System.out.println("Please pick your test to run.");
System.out.println("population");
System.out.println("income");
System.out.println("password");
System.out.println("randomize");
System.out.print("----->: ");
test_run = sc.next();
if (!"population".equals(test_run) && !"income".equals(test_run) && !"password".equals(test_run) && !"randomize".equals(test_run)) {
LOOP_2 = 0;
}
if ("population".equals(test_run) & "income".equals(test_run) & "password".equals(test_run) & "randomize".equals(test_run)) {
LOOP_2 = 1;
}
**
}
if ("population".equals(test_run)) {
}
if ("income".equals(test_run)) {
}
if ("password".equals(test_run)) {
}
if ("randomize".equals(test_run)) {
System.out.println("Enter the number of names that will be randomized.");
System.out.print("----->: ");
name_num = sc.nextInt();
}
}
}
【问题讨论】:
-
现在是学习如何正确使用
debugger的好时机。在loops的不同点启动一些断点并测试以查看导致问题的变量。 -
是的,一个调试器。放置一些断点,如果你使用 == 来比较字符串,用 equals() 替换它。 (但在撰写此评论时,我还没有找到任何带字符串的 ==)
标签: java while-loop options choice