DoneHealing
Programmer
Hi, I'm having some problems with a program I'm working on and was hoping that someone here might be able to help me out.
The purpose of program is to ask the user to enter number guesses into an array and then compare this with an array of random generated numbers to find out how many of the user's guesses were correct.
I really don't know how to go about comparing two arrays. I've looked up the equals() method but I don't really get how it works.
I thought maybe I'd have to make a loop to compare each element of the arrays?
This is what I have so far:
Could anyone help me out with how I could go about doing this?
The purpose of program is to ask the user to enter number guesses into an array and then compare this with an array of random generated numbers to find out how many of the user's guesses were correct.
I really don't know how to go about comparing two arrays. I've looked up the equals() method but I don't really get how it works.
I thought maybe I'd have to make a loop to compare each element of the arrays?
This is what I have so far:
Code:
import javax.swing.JOptionPane;
import java.util.Random;
public class NumberGuess {
public static void main ( String args[] )
{
int[] num = new int[6];
int[] randnum = new int [6];
for (int i = 0; i < num.length; i++)
{
num[i]=Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a guess between 1 and 6 "));
do {
if (num[i]<1 || num[i]>6) {
JOptionPane.showMessageDialog (null, "That is not a valid guess, please try again ");
num[i]=Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a number between 1 and 6 "));
}
}while (num[i]<1 || num[i]>6);
}
String strArray1 = "";
for (int i = 0; i < num.length; i++)
strArray1 = strArray1 + num[i] + " ";
JOptionPane.showMessageDialog (null, "Your guesses are: " +strArray1);
Random rn = new Random();
for (int i = 0; i < randnum.length; i++)
{
randnum[i] = rn.nextInt(6)+1;
}
String strArray = "";
for (int i = 0; i < randnum.length; i++)
strArray = strArray + randnum[i] + " ";
JOptionPane.showMessageDialog (null, "The random numbers are: " +strArray);
}
}
Could anyone help me out with how I could go about doing this?