NARASHIMA M
NARASHIMA M

Reputation: 11

How to make this program to accept n number of test cases?

I have been practising dynamic coding, So here I want to accept n number of test cases, but when I enter more than one number of testcases it only accepts only one test case . Lets say I enter the number of test cases as 2 and the number of player and villains as 3 and entered their energies but it only runs for one iteration and the program ends on the next iteration. How to make it to accept many test cases?

/* Read input from STDIN. Print your output to STDOUT*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
class Testcase // Name should be in PASCAL
{
    int N;
    int [] play;
    int [] villain;
    String status;

    public Testcase (int n) { // Constructor
        this.N=n;
        play = new int [N];
        villain=new int [N];
    }

}
public class Main {
    static List<Testcase> caseno=new ArrayList<Testcase>();
    public static void main(String args[] ) throws Exception {
        List<Testcase> caseno=new ArrayList<Testcase>();
        Scanner sc=new Scanner(System.in);
        //int n1=10;
        System.out.println("Enter the number of Test Cases:");
        int n1=sc.nextInt();
        int i,j;
        Testcase t;
        for(i=0;i<n1;i++)
        {
            //int n=6;
            System.out.println("Enter the number of players and villains:");
            int n=sc.nextInt();
            t=new Testcase(n);

            System.out.println(t.N+" "+t.play.length);
            System.out.println("Enter the Player Energies");
            for(i=0;i<n;i++)
            {
                t.play[i]=sc.nextInt();
            }
            System.out.println("Enter the Villain Energies");
            for(i=0;i<n;i++)
            {
                t.villain[i]=sc.nextInt();
            }
            Arrays.sort(t.play);
            Arrays.sort(t.villain);
        }
        for(Testcase t1:caseno)
        {
            for(i=0;i<t1.N;i++)
            {
                System.out.println(t1.villain[i]+" "+t1.play[i]);
            }
            int flag=0;
            for(i=0;i<t1.N;i++)
            {
                if(t1.play[i]<t1.villain[i])
                {
                    flag=0;
                }
                else
                {
                    flag=1;
                    t1.status="LOSS";
                    break;
                }
            }
            if(flag==0)
            {
                t1.status="WON";
            }
            System.out.println(t1.status);

        }



        for (Testcase j1:caseno)
        {
            System.out.println(j1.status);

        }

        //Write code here

    }
}

Upvotes: 0

Views: 1505

Answers (1)

Melvin
Melvin

Reputation: 1041

The problem in your case is the visibility of variables. I will show you the problem in a short way:

int i;
for (i=0; i<2; i++) {        // loop 1
   for (i=0; i<3; i++) {     // loop 2
       // do something
   }
}

In loop2 the variable i will run from 0 to 2. After the exit of the 2nd loop the variable "i" holds the number 3 (that's why it exited). So loop 1 will exit as well because the end-condition has been reached (i<2). But that is not what you want.

It's better to surpass the problem like that:

for (int i1=0; i1<2; i1++) {     // loop 1
   for (int i2=0; i2<3; i3++) {  // loop 2
      // do something
   }
}

This way every variable holds its own scope and will not interfere with other variables who have the same name. It will run through loop2 ("i2" ==0, ==1 and ==2) now two times (one for "i1"==0 and one for "i1"==1).

And that is the basic problem in your example.

You can find more informations here:

Upvotes: 1

Related Questions