java中怎么实现从键盘输入数据然后添加给数组
2个回答
展开全部
建议使用List,然后再从List转成数组。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
String str = scanner.nextLine();
if(str.equals("exit")) {
break;
}
list.add(str);
}
String[] arr = new String[list.size()];
arr = list.toArray(arr);
for(String a : arr)
System.out.println(a);
}
}
输入exit为退出从键盘输入数据并打印数组
展开全部
import java.util.Arrays;
import java.util.Scanner;
public class dayfour {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入个数:");
int a = input.nextInt();
int[] arr = new int[a];
for (int i = 0; i < arr.length; i++) {
System.out.println("请输入第" + (i + 1) + "个数");
arr[i] = input.nextInt();
}
int arr1[] = {a,1,2,2,2};
System.out.println(Arrays.toString(arr1));
Scanner input1 = new Scanner (System.in);
System.out.println("请输入数字");
int c = input1.nextInt();
int [] ACC =new int[c];
for (int i = 0; i < ACC.length; i++) {
System.out.println("请输入第" + (i+1) + "号");
ACC[i] = input.nextInt();
}
System.out.println(Arrays.toString(ACC));
}
}
import java.util.Scanner;
public class dayfour {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入个数:");
int a = input.nextInt();
int[] arr = new int[a];
for (int i = 0; i < arr.length; i++) {
System.out.println("请输入第" + (i + 1) + "个数");
arr[i] = input.nextInt();
}
int arr1[] = {a,1,2,2,2};
System.out.println(Arrays.toString(arr1));
Scanner input1 = new Scanner (System.in);
System.out.println("请输入数字");
int c = input1.nextInt();
int [] ACC =new int[c];
for (int i = 0; i < ACC.length; i++) {
System.out.println("请输入第" + (i+1) + "号");
ACC[i] = input.nextInt();
}
System.out.println(Arrays.toString(ACC));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询