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: xyzNames on the list: xyzWant to continue? (Y/N): YEnter the name: abcNames on the list: xyz,abcWant to continue? (Y/N): NEnd of Program
添加回答
举报
0/150
提交
取消