需要新手帮助。目的是从用户输入加载数组。当用户键入" EXIT"他们应该退出循环,但我的字符串比较不起作用。任何建议将不胜感激。
//Declare Variables
String[] a = new String[50];
String CompareValue = "";
String CondChecker = "EXIT";
Scanner sc = new Scanner(System.in);
//Prompt user for input
System.out.println("What is the name? ");
//Load array
do {
for(int j=0; j<50; j++){
a[j]= sc.next();
CompareValue = a[j];
System.out.println("Another name? ");
CompareValue = a[j];
}
//Check exit condition
} while(!CompareValue.equals(CondChecker));
答案 0 :(得分:0)
取出for
循环
int j = -1;
do {
j++;
a[j]= sc.next();
CompareValue = a[j];
System.out.println("Another name? ");
CompareValue = a[j];
//Check exit condition
} while(!CompareValue.equals(CondChecker));
答案 1 :(得分:0)
你的检查是打破do循环。但是如果输入是EXIT,你仍然没有打破for循环的50次迭代。