输入n个数为一个数组, 不能提前输入数组长度
1.
复制代码
1当字符串转为单个字符,再转为单个字符串,在转为int型
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13public 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.
复制代码
1
2
3
4
5
6
7
8public 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
复制代码
1
2
3
4
5
6
7
8Scanner 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]); }
最后
以上就是光亮大地最近收集整理的关于输入n个整数(数组,输入的数必须全部为数组)(String 转化为int数组)的全部内容,更多相关输入n个整数(数组,输入的数必须全部为数组)(String内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复