在多行中获取单个数组中的输入(测试用例)
Taking Inputs in single array in multiple lines (Test Cases)
我可以通过下面的代码在数组中的一行中获取输入,但我希望它是-
3 //测试用例数
640 480 // 换行
120 300 // 换行
180 180 // 换行
"3"是测试用例的个数,这6个数字需要存储在一个数组中,怎么办?
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));
你可以这样试试,
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++;
}
我可以通过下面的代码在数组中的一行中获取输入,但我希望它是-
3 //测试用例数
640 480 // 换行
120 300 // 换行
180 180 // 换行
"3"是测试用例的个数,这6个数字需要存储在一个数组中,怎么办?
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));
你可以这样试试,
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++;
}