【发布时间】:2020-04-04 09:20:15
【问题描述】:
用户在 System.in 中键入
1 2 3 4 44 50 and so on
我需要从中获取int数组,所以我的解决方案是:
import java.util.Scanner;
public class Program
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String tempString = in.nextLine();
int tempArray[] = tempString.split(" ");
result[] = tempArray(tempArray.length);
for(int i=0; i<result.length; i++)
{
result[i] = Integer.parseInt(tempArray[i]);
}
}
}
有没有办法简化代码?
【问题讨论】:
-
您的代码没有通过编译 -
tempArray应该是 String 数组,而不是 int 数组。而tempArray(tempArray.length)也没有通过编译。
标签: java arrays input int system