Java中把若干的数字转换为整型数组,用Scanner扫描了数字输入后怎么转换为数组并输出?
1个回答
展开全部
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] arr={3,6,5,8,4,7,2};
//用scanner方法输入1,新数列变成int[] arr={1,3,6,5,8,4,7,2},并能排序输出
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int[] arr2 = new int[arr.length + 1];
Arrays.fill(arr2,0);
System.out.println(Arrays.toString(arr2));
arr2 = Arrays.copyOf(arr,arr.length + 1);
arr2[arr.length] = number;
System.out.println(Arrays.toString(arr2));
Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] arr={3,6,5,8,4,7,2};
//用scanner方法输入1,新数列变成int[] arr={1,3,6,5,8,4,7,2},并能排序输出
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int[] arr2 = new int[arr.length + 1];
Arrays.fill(arr2,0);
System.out.println(Arrays.toString(arr2));
arr2 = Arrays.copyOf(arr,arr.length + 1);
arr2[arr.length] = number;
System.out.println(Arrays.toString(arr2));
Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询