2016-02-16 117 views
0

我会在这里粘贴所有的代码。我一直在研究这一点。得到它终于工作,但它只能通过第一个循环,输入大部分数据,一次。如果之前询问过,只是引导我搜索什么。程序只循环一次,但它应该循环T次

public class Sol { 

    public static void main(String[] args) { 

     List<List<List<Integer>>> testCases = new ArrayList<>(); 
     List<List<Integer>> list = new ArrayList<>(2); 

     int T, N, K, a; 

     Scanner input = new Scanner(System.in); 
     System.out.println("Enter Number of Test Cases: "); 
     T = input.nextInt(); 

     for(int count=0; count<T; count++){ 
      for(int c=0; c<2; c++){ 
       list.add(new ArrayList<>()); 
      } 
      testCases.add(list); 
     } 

     for(int count=0; count<T; count++){ 
      System.out.println("Enter Number of Students and Cancellation Threshold: "); 
      N = input.nextInt(); 
      K = input.nextInt(); 

      testCases.get(count).get(0).add(N); 
      testCases.get(count).get(1).add(K); 

      System.out.print("Enter Arrival Times: "); 
      int early = 0; 
      for(int c=0; c<N; c++){ 
       List<Integer> list1 = new ArrayList<>(N); 
       for (int c1=0; c1<N; c1++){ 
        a = input.nextInt(); 
        list1.add(a); 
        if(a<=0){ 
         early++; 
        } 
       } 
       list.add(list1); 
       System.out.println("Is Class Cancelled?"); 
       if(early>=K){ 
        System.out.print("No"); 
       } 
       else 
        System.out.print("Yes"); 
      } 
     } 
     System.exit(0); 
    } 
} 
+0

更具体,它做了什么,你期望它做什么,对于哪些输入,......?你可能想开始改进你的代码。 int T ...这是什么意思?根据命名约定为变量赋予有意义的名称。对于大多数开发人员,T将是一个通用类型 – Stultuske

+0

您是否可以包含一些示例输入以及实际发生的地方? – SomeJavaGuy

+0

它应该检索一些测试用例。在每个测试案例中,我会输入一定数量的学生和门槛。门槛值为有多少学生应该上课。在我输入的值等于学生人数之后,如果低于0,他们会提前,如果超过0,他们会迟到。我得到的人数是多少,如果是这样,它打印“否”表示课程没有取消,否则是。它只遍历第一测试用例并停止 – user5919465

回答

1

你有一个内环,其请求输入,并且输出“是”或“否”:

 for(int c=0; c<N; c++){ 
      List<Integer> list1 = new ArrayList<>(N); 
      for (int c1=0; c1<N; c1++){ 
       a = input.nextInt(); 
       // Requires input! 
       // ... 
      } 
      // ... 
      System.out.println("Is Class Cancelled?"); 
      if(early>=K){ 
       System.out.print("No"); 
      } 
      else 
       System.out.print("Yes"); 
     } 

所以,一旦印有“否”,它会去循环的下一次迭代并读取更多输入。

程序不会无法再次循环 - 它卡在内部循环中,等待更多输入。