简体   繁体   中英

Program only loops once yet it's supposed to loop T times

I'll paste all the code here. I've been working on this for a while. Got it to finally work but it only goes through the first loop, where input most of the data, once. If it's been asked before just guide me on what exactly to search for.

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

You have an inner loop, which requests input, and which outputs "Yes" or "No":

        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");
        }

So, once it has printed "No", it will go to the next iteration of the loop and read more input.

The program isn't failing to loop again - it is stuck in the inner loop, waiting for more input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM