从 java 中的字符串数组中的用户获取输入
Taking Input From User In Array Of String in java
以下是我的代码片段,我在循环的最后一行。
public static void main(String[] args) {
String [] name = new String [5];
Scanner get = new Scanner (System.in);
for (int i=0; i<name.length; i++) {
System.out.println("Enter Name In This Array Of String ");
[i]name = get.nextLine(); // Here
}
如果你有 pre-defined 个输入,你可以按原样使用数组,定义为 run-time 你也可以使用 ArrayList
public static void main(String[] args) {
String [] name = new String [5];
Scanner get = new Scanner (System.in);
for (int i=0; i<name.length; i++) {
System.out.println("index = "+i);
System.out.println("Enter Name In This Array Of String ");
name[i] = get.nextLine(); // Here
}
}
这里是动态名称示例
public static void main(String[] args) {
List<String> name = new ArrayList<String>();
Scanner get = new Scanner (System.in);
boolean quit=false;
while(!quit){
System.out.println("Enter q to quit or Name In This Array Of String ");
String input = get.nextLine();
if(input.equals("q")){
quit=true;
}else{
name.add(input); // Here
}
}
System.out.println("Array Final Values = "+name);
}
以下是我的代码片段,我在循环的最后一行。
public static void main(String[] args) {
String [] name = new String [5];
Scanner get = new Scanner (System.in);
for (int i=0; i<name.length; i++) {
System.out.println("Enter Name In This Array Of String ");
[i]name = get.nextLine(); // Here
}
如果你有 pre-defined 个输入,你可以按原样使用数组,定义为 run-time 你也可以使用 ArrayList
public static void main(String[] args) {
String [] name = new String [5];
Scanner get = new Scanner (System.in);
for (int i=0; i<name.length; i++) {
System.out.println("index = "+i);
System.out.println("Enter Name In This Array Of String ");
name[i] = get.nextLine(); // Here
}
}
这里是动态名称示例
public static void main(String[] args) {
List<String> name = new ArrayList<String>();
Scanner get = new Scanner (System.in);
boolean quit=false;
while(!quit){
System.out.println("Enter q to quit or Name In This Array Of String ");
String input = get.nextLine();
if(input.equals("q")){
quit=true;
}else{
name.add(input); // Here
}
}
System.out.println("Array Final Values = "+name);
}