import java.util.*;
public class Test {
public static void main(String[] args) {
ArrayList<String> name = new ArrayList<String>();
String cond="y";
do {
Scanner n = new Scanner(System.in);
System.out.println("enter the name: ");
String value = n.nextLine();
name.add(value);
Iterator itr = name.iterator();
while(itr.hasNext()) {
System.out.println("Names on the list: "+itr.next());
}
System.out.println("Want to continue? (Y/N): ");
cond=n.nextLine();
} while(cond=="Y");
}
System.out.println("End of Program");
}
这是获取名称并将它们存储在列表中的代码。然后它必须打印存储在列表中的所有这些值。然后它必须询问用户他们是否有更多输入,如果“是”那么它必须获取值,存储它,显示所有名称。
但它在 while 循环中不起作用。谁能给我解释一下?
预期输出:
Enter the name: xyz
Names on the list: xyz
Want to continue? (Y/N): Y
Enter the name: abc
Names on the list: xyz,abc
Want to continue? (Y/N): N
End of Program
Cats萌萌
相关分类