输入n个数为一个数组, 不能提前输入数组长度
1.
当字符串转为单个字符,再转为单个字符串,在转为int型
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int[] num=new int[str.length()];
for (int i = 0; i <num.length ; i++) {
// 当字符串转为单个字符,再转为单个字符串,在转为int型
// Integer.parseInt和Integer.valueOf 意思相同
num[i]=Integer.parseInt(String.valueOf(str.charAt(i)));
num[i]=Integer.valueOf(String.valueOf(str.charAt(i)));
}
2.
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int[] num=new int[str.length()];
for (int i = 0; i <num.length ; i++) {
num[i]=Integer.valueOf(str.charAt(i))-'0';
}
}
3.用split
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String[] str1=str.split(" ");
int[] num=new int[str1.length];
for (int i = 0; i <num.length ; i++) {
num[i]=Integer.valueOf(str1[i]);
}