这只是我挑战自己解决的一个简单问题:将名称添加到 ArrayList。我认为问题与我用来填充 ArrayList 和扫描仪的 while 循环有关。我试过制作两个单独的Scanner对象,但循环仍然在不应该终止的时候终止。import java.util.ArrayList;import java.util.Scanner;public class MyArrayLiat { public static void main(String[] args) { // TODO Auto-generated method stub String finished = "n"; Scanner keyboard = new Scanner(System.in); ArrayList<String> myArrayList = new ArrayList<String>(); while(finished == "n") { System.out.println("Enter data: "); myArrayList.add(keyboard.nextLine()); System.out.println("Are you finished? y or n"); finished = keyboard.nextLine(); } System.out.println("myArrayList: " + myArrayList); }}Enter data: TrevorAre you finished? y or nnmyArrayList: [Trevor]在我键入 n 之前,它应该一直取名字,但它只在名字后终止。
添加回答
举报
0/150
提交
取消