• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array problem

 
Mike Pirrone
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey whats up everyone? I'm trying to create a program that loads an array of 6 numbers via random numbers, then compares those numbers with another 6 for a possible match ala (lotto). my problem is, I'm trying to figure out how to output a message if 6 of 6 numbers are the same, 5 of 6, 4 of 6, etc. Here is my source code:
public class Lotto2
{
public static void main(String[] args) throws Exception
{

String inputString = new String();
char newChar;
int inputNumber;
int[] lotto = new int[6];
System.out.println("How Many Games would you like to play?");
newChar = (char)System.in.read();
while(newChar >= '0' && newChar <= '9')
{
inputString = inputString + newChar;
newChar = (char)System.in.read();
}
inputNumber = Integer.parseInt(inputString);
System.in.read();
//load for duplicates
for (int x = 0; x < lotto.length; x ++)
{
lotto[x] = (int)(Math.random() * 53 + 1);
if (x > 0) // checking random numbers
{
for (int y = 0; y < x; y ++)
if (lotto[y] == lotto [x])
{
lotto[x] = (int)(Math.random() * 53 +1);
y = 0;

}
}
}
//Bubble Sort
int arrlen = lotto.length-1;
for(int x = 0; x <arrlen; x++)>
{
for(int y = 0; y < arrlen - x; y++)
{
if (lotto[y] > lotto[y+1])
{
int temp;
temp = lotto[y];
lotto[y] = lotto[y+1];
lotto[y+1] = temp;
}
}
}
// printing array
for (int x = 0; x < lotto.length; x++)
{
System.out.print(lotto[x]+ " ");
}
System.out.println();
}

}
This is the source code I have worked on with my professor so far and as you can see after I figure this problem out I'm trying to come up with a way for the user to generate the number of times they would like to play. How do I tackle this problem? Do I do I switch statement? Thank you for all your help!
-Mike-
 
Jeff Slominsky
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after loading numbers up in an array via a random number generator, the best thing to do would be to
1. use the random generator in a for loop, and fill array 1 with 6 values.
2. do the same for the second array.
3. use a third array of booleans to compare the numbers of the previous two arrays,
4.use the boolean array in an loop with an if statment and print out the resuts of each of the 6 comparisons, using true or false
to do the responce,
put your code into a while loop and put a condition on the top, like (
while (responce.equals.toUpperCase("YES"))
and that will do the trick, good luck
Jeff
---------------------
SCJP, CS student
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic