我可以通过下面的代码在数组中的单行输入,但我希望它是 -
3 //没有测试用例
640 480 //新行
120 300 //新行
180 180 //新行
“3”是测试用例中的一个,六个数字需要存储在一个数组中,我应该怎么做?
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Min Length:-");
String lt = br.readLine(); //Ignore this
int length= Integer.parseInt(lt);
System.out.println("Enter Test Cases:-");
String temp = br.readLine(); //test-case input
int testcases = Integer.parseInt(temp);
System.out.println("Enter the W and H");
String array = br.readLine(); //this takes only input in single line
String No[] = array.trim().split("\\s+");
int intarray[]= new int[testcases];
for(int i =0;i<intarray.length;i++)
{
intarray[i]=Integer.parseInt(No[i]);
}
System.out.println(Arrays.toString(intarray));
答案 0 :(得分:0)
你可以这样试试,
int testcases = Integer.parseInt(temp);
int i=0;
List<Integer> list = new ArrayList<Integer>();
while(i<=testcases)
{
System.out.println("Enter the W and H");
String array = br.readLine(); //this takes only input in single line
String No[] = array.trim().split("\\s+");
//int intarray[]= new int[testcases];
for(int i =0;i<No.length;i++)
{
//intarray[i]=Integer.parseInt(No[i]);
list.add(Integer.parseInt(No[i]));
}
Integer[] intArray = list.toArray(new Integer[0]);
System.out.println(Arrays.toString(intarray));
i++;
}