为什么我必须写两次才能在 Arraylist 中添加一个输入?
Why do I have to write twice to add an input in the Arraylist?
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter a product");
String product = input.nextLine();
arrayList.add(product);
}
while (!input.nextLine().equalsIgnoreCase("q"));
System.out.println("You wrote the following products \n");
for (String naam : arrayList) {
System.out.println(naam);
}
}
我正在尝试从用户那里获取一些输入并将它们存储到 arraylist 中。问题是我必须将项目写两次才能将项目添加到列表中。我不明白为什么!
每写readLine()
,就会读一行。在这个循环中,
do {
System.out.println("Enter a product");
String product = input.nextLine();
arrayList.add(product);
}
while (!input.nextLine().equalsIgnoreCase("q"));
readLine()
出现了两次,因此每次迭代读取两行。第一行总是添加到列表中,而不是根据 q
检查,第二行永远不会添加到列表中,并且总是根据 q
.
检查
你应该只做 nextLine
一次:
while (true) {
System.out.println("Enter a product");
String product = input.nextLine(); // only this one time!
if (!product.equalsIgnoreCase("q")) {
arrayList.add(product);
} else {
break;
}
}
碰巧是因为 input.nextLine()
让 java 读取了输入。您应该阅读该行,然后才执行以下操作:
Scanner input = new Scanner(System.in);
String product = input.nextLine();
System.out.println("Enter a product");
while (!product.equalsIgnoreCase("q")) {
arrayList.add(product);
System.out.println("Enter a product");
product = input.nextLine();
}
仅使用 while
而不是 do-while
循环
while (true){
System.out.println("Enter a product");
String product = input.nextLine();
if (!product.equalsIgnoreCase("q"))
arrayList.add(product);
else
break;
}
您可以使用 input.next() 一次读取 String 值,并使用 while 循环,仅当该值不是等于 q。
如果你像你的情况一样阅读了两次,一个值会添加到你的 do 部分的列表中,而你在 while 部分再次读取的值仅与 q 进行比较,因此要退出你的代码,你将错过一个值并添加另一个并且必须一个接一个地给出两个 q 值才能退出它。
此外,由于大多数其他用户使用 nextLine 而不是 next 给出了答案,您可能想检查 next 和 nextLine 做了什么。简而言之,如果您输入由定界符分隔的产品名称(默认为 space),那么接下来,每个由 space 分隔的值都被视为一个产品。同样,如果您在不同的行上输入也是如此。但是,对于 nextLine,每一行作为一个整体都将作为一个新产品添加。这取决于您可能希望如何根据您的要求实现此目的。
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
Scanner input = new Scanner(System.in);
String product = input.next();
while(!product.equalsIgnoreCase("q")) {
arrayList.add(product);
product = input.next();
}
System.out.println("You wrote the following products \n");
for (String naam : arrayList) {
System.out.println(naam);
}
}
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter a product");
String product = input.nextLine();
arrayList.add(product);
}
while (!input.nextLine().equalsIgnoreCase("q"));
System.out.println("You wrote the following products \n");
for (String naam : arrayList) {
System.out.println(naam);
}
}
我正在尝试从用户那里获取一些输入并将它们存储到 arraylist 中。问题是我必须将项目写两次才能将项目添加到列表中。我不明白为什么!
每写readLine()
,就会读一行。在这个循环中,
do {
System.out.println("Enter a product");
String product = input.nextLine();
arrayList.add(product);
}
while (!input.nextLine().equalsIgnoreCase("q"));
readLine()
出现了两次,因此每次迭代读取两行。第一行总是添加到列表中,而不是根据 q
检查,第二行永远不会添加到列表中,并且总是根据 q
.
你应该只做 nextLine
一次:
while (true) {
System.out.println("Enter a product");
String product = input.nextLine(); // only this one time!
if (!product.equalsIgnoreCase("q")) {
arrayList.add(product);
} else {
break;
}
}
碰巧是因为 input.nextLine()
让 java 读取了输入。您应该阅读该行,然后才执行以下操作:
Scanner input = new Scanner(System.in);
String product = input.nextLine();
System.out.println("Enter a product");
while (!product.equalsIgnoreCase("q")) {
arrayList.add(product);
System.out.println("Enter a product");
product = input.nextLine();
}
仅使用 while
do-while
循环
while (true){
System.out.println("Enter a product");
String product = input.nextLine();
if (!product.equalsIgnoreCase("q"))
arrayList.add(product);
else
break;
}
您可以使用 input.next() 一次读取 String 值,并使用 while 循环,仅当该值不是等于 q。 如果你像你的情况一样阅读了两次,一个值会添加到你的 do 部分的列表中,而你在 while 部分再次读取的值仅与 q 进行比较,因此要退出你的代码,你将错过一个值并添加另一个并且必须一个接一个地给出两个 q 值才能退出它。 此外,由于大多数其他用户使用 nextLine 而不是 next 给出了答案,您可能想检查 next 和 nextLine 做了什么。简而言之,如果您输入由定界符分隔的产品名称(默认为 space),那么接下来,每个由 space 分隔的值都被视为一个产品。同样,如果您在不同的行上输入也是如此。但是,对于 nextLine,每一行作为一个整体都将作为一个新产品添加。这取决于您可能希望如何根据您的要求实现此目的。
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
Scanner input = new Scanner(System.in);
String product = input.next();
while(!product.equalsIgnoreCase("q")) {
arrayList.add(product);
product = input.next();
}
System.out.println("You wrote the following products \n");
for (String naam : arrayList) {
System.out.println(naam);
}
}