Jump to content

How to compare two java arrays ?


jamesy

Recommended Posts

Hi guys so here is a project i am working on , i have  two arrays array "l1" and "s1" bacially "l1" holds the answers to the questions asked for the maths questions ,, when the person answers the question it is loaded into the other array "s1" from the scanner now i want to compare theses two arrays to see how much questions are right and how much are wrong therefore printing out some thing like "Wrong" and "Right", does any one have any ideas how i could go about it ? really stuck thanks guys for any help

import java.util.Scanner;
public class Math { 
public static void  main(String[] args) {
Scanner myScanner = new Scanner(System.in);
 	 boolean con = true;  
      int level;
      // main values
      int l1[] ={1,2,3,4,5,6,7,8,9,10,11,12,2,4,6,8,10,12,14,16,18,20,22,24,3,6,9,12,15,18,21,24,27,30,33,36};
      int s1[]= new int[36]; 
      
     
      	/////////////.....................................................................
      	 
                 System.out.println("Hello Welcome To The Maths Test !!");
                 System.out.println();
                 System.out.println("Best Of Luck !!");
                 System.out.println();
                 System.out.println("Please Select Your Difficulity !!");
                 System.out.println();
                 System.out.print("Type 1 For Easy / Type 2 For Medium / Type 3 For Hard!!");
                 System.out.println();
                 ///////// choices.....................................................
       level = myScanner.nextInt();
       // Easy
       if (level ==1 )
       {
       while ( level ==1 )
       {
  	   System.out.println("You Have Selected Stage 1 ");
  	   level =2;
  	   System.out.println("Best Of Luck In Stage 1 ");
  	   //////times one tables
  	   System.out.print("What is  1 x 1 = "); l1[0] = myScanner.nextInt();    System.out.print("What is  1 x 2 = "); l1[1] = myScanner.nextInt();
       System.out.print("What is  1 x 3 = "); l1[2] = myScanner.nextInt();    System.out.print("What is  1 x 4 = "); l1[3] = myScanner.nextInt();
  	   System.out.print("What is  1 x 5 = "); l1[4] = myScanner.nextInt();    System.out.print("What is  1 x 6 = "); l1[5] = myScanner.nextInt();
  	   System.out.print("What is  1 x 7 = "); l1[6] = myScanner.nextInt();    System.out.print("What is  1 x 8 = "); l1[7] = myScanner.nextInt();
       System.out.print("What is  1 x 9 = "); l1[8] = myScanner.nextInt();    System.out.print("What is  1 x 10 = "); l1[9] = myScanner.nextInt();
  	   System.out.print("What is  1 x 11 = "); l1[10] = myScanner.nextInt();  System.out.print("What is  1 x 12 = "); l1[11] = myScanner.nextInt();
  	   // Times 2 Tables -----------------------------------------------------------------------------------------------------------------------
  	   System.out.print("What is  2 x 1 = "); l1[12] = myScanner.nextInt();   System.out.print("What is  2 x 2 = "); l1[13] = myScanner.nextInt();
       System.out.print("What is  2 x 3 = "); l1[14] = myScanner.nextInt();   System.out.print("What is  2 x 4 = "); l1[15] = myScanner.nextInt();
  	   System.out.print("What is  2 x 5 = "); l1[16] = myScanner.nextInt();   System.out.print("What is  2 x 6 = "); l1[17] = myScanner.nextInt();
  	   System.out.print("What is  2 x 7 = "); l1[18] = myScanner.nextInt();   System.out.print("What is  2 x 8 = "); l1[19] = myScanner.nextInt();
       System.out.print("What is  2 x 9 = "); l1[20] = myScanner.nextInt();   System.out.print("What is  2 x 10 = "); l1[21] = myScanner.nextInt();
       System.out.print("What is  2 x 11 = "); l1[22] = myScanner.nextInt();  System.out.print("What is 2 x 12 = "); l1[23] = myScanner.nextInt();
       	// three times tables ----------------------------------------------------------------------------------------------------------------
       System.out.print("What is  3 x 1 = "); l1[24] = myScanner.nextInt();   System.out.print("What is  3 x 2 = "); l1[25] = myScanner.nextInt();
       System.out.print("What is  3 x 3 = "); l1[26] = myScanner.nextInt();   System.out.print("What is  3 x 4 = "); l1[27] = myScanner.nextInt();
       System.out.print("What is  3 x 5 = "); l1[28] = myScanner.nextInt();   System.out.print("What is  3 x 6 = "); l1[29] = myScanner.nextInt();
       System.out.print("What is  3 x 7 = "); l1[30] = myScanner.nextInt();   System.out.print("What is  3 x 8 = "); l1[31] = myScanner.nextInt();
       System.out.print("What is  3 x 9 = "); l1[32] = myScanner.nextInt();   System.out.print("What is  3 x 10 = "); l1[33] = myScanner.nextInt();
       System.out.print("What is  3 x 11 = "); l1[34] = myScanner.nextInt();  System.out.print("What is  3 x 12 = "); l1[35] = myScanner.nextInt();
      
      
       }}
  	 
  	 /// maths here //                            
  	  
  // medium
  else if (level ==2)
  {while (level==2){
  	System.out.print("You Have Selected Stage 2");
  	level=3;
  }
  }
  
  // medium
  else if (level ==3)
  {while (level==3){
  	System.out.print("You Have Selected Stage 3");
  	level=4;
  }
  }
  
   else if (level !=1 && level!=2 && level!=3)
  {while (level !=1 && level!=2 && level!=3 ){
  	System.out.print("Please Select The Correct Stage Between 1-3");
  	level=1;
  }}
 
  }}

 

 

Share this post


Link to post
Share on other sites

  • 2 weeks later...

I'm not sure if I understand your problem. I think You want to tell the user/player if his/her answers are correct or not.

 

for(int i=0 ; i<36 ; i++){
	if(l1[i]==s1[i])
		System.out.println("Answer "+i+" is correct");
	else
		System.out.println("Answer "+i+" is incorrect");
}

 

You have to check the array containing the correct answers and the one containing the user/players selected answers position by position and informing.

Share this post


Link to post
Share on other sites

Building on what Fragsman said, you could also tell the user the correct answer using the array aswell (I think, using my very limited knowledge of Java);

 

This assumes you place the questions in a String array named 'q1'

 

for(int i=0 ; i<36 ; i++){
	if(l1[i]==s1[i])
		System.out.println("Answer " + i + " is correct");
	else
		System.out.println("Answer " + i + " (" + q1[i] + ") is incorrect. You put " + l1[i] + ". The correct answer was " + s1[i]);
}

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...