Java 扫描器要求我输入两次输入,只有一个需要注册
在过去的几个小时里,我对此做了大量的研究,但运气不佳。我很确定这是.next()或.nextLine()的问题(根据我的搜索)。然而,没有什么能帮我解决我的问题 当我运行下面的代码时,我必须输入两次输入,并且只有一个输入随后被添加到arrayList(这可以在打印arrayList的内容时看到)Java 扫描器要求我输入两次输入,只有一个需要注册,java,user-input,next,Java,User Input,Next,在过去的几个小时里,我对此做了大量的研究,但运气不佳。我很确定这是.next()或.nextLine()的问题(根据我的搜索)。然而,没有什么能帮我解决我的问题 当我运行下面的代码时,我必须输入两次输入,并且只有一个输入随后被添加到arrayList(这可以在打印arrayList的内容时看到) 导入java.io.File; 导入java.util.ArrayList; 导入java.util.Scanner; 公共类测试员{ 公共静态void main(字符串[]args){ AddStri
导入java.io.File;
导入java.util.ArrayList;
导入java.util.Scanner;
公共类测试员{
公共静态void main(字符串[]args){
AddStrings();
}
公共静态void AddStrings(){
扫描仪控制台=新扫描仪(System.in);
ArrayList strings=new ArrayList();//此ArrayList将保存用户在下面的while循环中键入的输入
while(true){
System.out.println(“输入文件名(无空格)(键入完成):”;
如果(console.next().equals(“done”))中断;
//console.nextLine();/*根据我的观察,每次使用.next()或.nextLine(),我都需要再次输入相同的输入
//*然而,我所有的google/stackoverflow/reddit搜索据说都包括
//*a.nextLine()*/
//String inputs=console.next();//.next让我输入两次,.nextLine只让我输入一次,但不向arrayList添加任何内容
strings.add(console.next());
}
System.out.println(字符串);//用于测试目的
console.close();
}
}
在代码中,您要求插入两个单词。只需移除其中一个
这样使用它:
String choice = console.next();
if (choince.equals('done')) break;
strings.add(choice);
在代码中,您要求插入两个单词。只需移除其中一个 这样使用它:
String choice = console.next();
if (choince.equals('done')) break;
strings.add(choice);
代码的问题是您要执行两次console.next()。 第一个内部if条件和 在添加到阵列列表时第二个。 正确代码:
public class TestClass{
public static void main(String[] args) {
AddStrings();
}
public static void AddStrings() {
Scanner console = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<String>(); //this arraylist will hold the inputs the user types in in the while loop below
while(true) {
System.out.println("Input file name (no spaces) (type done to finish): ");
String input = console.next();
if(input.equals("done")) break;
strings.add(input);
System.out.println(strings);
}
System.out.println(strings); //for testing purposes
console.close();
}
}
公共类TestClass{
公共静态void main(字符串[]args){
AddStrings();
}
公共静态void AddStrings(){
扫描仪控制台=新扫描仪(System.in);
ArrayList strings=new ArrayList();//此ArrayList将保存用户在下面的while循环中键入的输入
while(true){
System.out.println(“输入文件名(无空格)(键入完成):”;
字符串输入=console.next();
如果(输入等于(“完成”))中断;
字符串。添加(输入);
System.out.println(字符串);
}
System.out.println(字符串);//用于测试目的
console.close();
}
}
您的代码的问题是您正在执行两次console.next()。
第一个内部if条件和
在添加到阵列列表时第二个。
正确代码:
public class TestClass{
public static void main(String[] args) {
AddStrings();
}
public static void AddStrings() {
Scanner console = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<String>(); //this arraylist will hold the inputs the user types in in the while loop below
while(true) {
System.out.println("Input file name (no spaces) (type done to finish): ");
String input = console.next();
if(input.equals("done")) break;
strings.add(input);
System.out.println(strings);
}
System.out.println(strings); //for testing purposes
console.close();
}
}
公共类TestClass{
公共静态void main(字符串[]args){
AddStrings();
}
公共静态void AddStrings(){
扫描仪控制台=新扫描仪(System.in);
ArrayList strings=new ArrayList();//此ArrayList将保存用户在下面的while循环中键入的输入
while(true){
System.out.println(“输入文件名(无空格)(键入完成):”;
字符串输入=console.next();
如果(输入等于(“完成”))中断;
字符串。添加(输入);
System.out.println(字符串);
}
System.out.println(字符串);//用于测试目的
console.close();
}
}