Jump to content


Photo
- - - - -

How to compare two java arrays ?

java

  • Please log in to reply
2 replies to this topic

#1 jamesy

jamesy

    New Member

  • Members
  • 12 posts

Posted 03 April 2013 - 09:22 AM

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

 

 



#2 Fragsman

Fragsman

    loco_frags

  • Members
  • PipPipPip
  • 705 posts
  • Gender:Male
  • Location:Argentina

Posted 11 April 2013 - 10:22 AM

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.


Desktop: Intel i7 950 3.0Ghz / ASUS P6X58D-E / 6Gb DDR3 Patriot 1600Mhz / WD Black Sata3 1Tb 7200RPM / Samsung B2030 20" / EVGA 460gtx 1Gb / Sound Blaster X-Fi XtremeGamer / Edifier R133 / Thermaltake v9 Black / Cooler Master 700w / DVD RW+ LG Black 22x Sata / Corsair Vengeance 1500 Headset
Laptop: AMD Athlon 64x2 TK57 1.9Ghz // 4GBRAM // HD 120 Gb 5400RPM // ATI x1250 128Mb // 15.4" Screen

My Creation below

membergk7.gif


#3 EuroFight

EuroFight

    I'm not lazy, I'm just energy efficient.

  • News Editor
  • PipPipPipPip
  • 1609 posts
  • Gender:Male
  • Location:London, UK

Posted 11 April 2013 - 11:05 AM

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

Processor AMD FX-6100 Hex-core, 3.3GHz > Intel Core 2 Duo, Dual-core, 1.6GHz

Memory 8GB Corsair Vengeance DDR3 1600MHz >  Crucial 3GB DDR3 1066MHz

Graphics Radeon HD7770 + Radeon HD5570 > Intel 3000 Integrated Graphics

Motherboard Gigabyte GA-970A-DS3 Socket AM3+ > OEM Latitude XT2 Motherboard Socket P

Storage Seagate Barracuda 2TB SATA 7200.14 > Samsung 64GB SSD SATA 3Gbps

Power Supply Cooler Master Elite ATX 500W > OEM Dell Power Supply 90W

Case Zalman Z11 Plus 4x 120mm fans, 3x 80mm > OEM Dell Latitude XT Case

 

"Sudo make me a sandwich" - BluePanda






Also tagged with one or more of these keywords: java