簡體   English   中英

多行輸入單個數組中的輸入(測試用例)

[英]Taking Inputs in single array in multiple lines (Test Cases)

我可以通過下面的代碼在數組的一行中接受輸入,但是我希望它是-

3 //沒有測試用例

640480 //新行

120300 //新行

180180 //新行

“ 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));

你可以這樣嘗試

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++;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM