Reputation: 73
I made a quick program recently to have the computer guess a number you input. I was just using it to show a friend an example of a While loop. I decided I wish to make it more complicated but I'm not sure how to do it.
I wish to add each random guess to an array so that it doesn't guess the same number more than once.
Scanner scan = new Scanner (System.in); // Number to guess //
Random rand = new Random(); // Generates the guess //
int GuessNum = 0, RandGuess = 0;
System.out.println("Enter a number 1 - 100 for me to guess: ");
int input = scan.nextInt();
if (input >= 1 && input <= 100)
{
int MyGuess = rand.nextInt (100) + 1;
while ( MyGuess != input)
{
MyGuess = rand.nextInt (100) + 1;
GuessNum++;
}
System.out.println ("I guessed the number after " + GuessNum + " tries.");
}
Upvotes: 3
Views: 1297
Reputation: 311
A very simple example would be:
public static int[] randomArray(){
int number = Integer.parseInt(JOptionPane.showInputDialog("How many numbers would you like to save?: ")); //Get Number of numbers from user
int[] array = new int[number]; //Create the array with number of numbers (by User)
for(int counter = 0; counter < number; counter++){ //For loop to get a new input and output every time
int arrayString = (int) (Math.random() * 10);
array[counter] = arrayString;
System.out.println("Number " + (counter + 1) + " is: " + array[counter]); //Print out the number
}
return array;
}
This example adds numbers every time the loop repeats, just replace the random with the number you want.
Upvotes: 2
Reputation: 5629
You might want to use an ArrayList
(A dynamically increasing array)
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(num);
If you want to see if that number is already added into the arraylist
if(list.contains(num))
{
System.out.println("You already tried " + num);
}
A Set
is also considerably better option. It is the same as a ArrayList
but doesn't allow duplicates.
HashSet<Integer> set = new HashSet<Integer>();
set.add(num);//returns true if the num was not inserted before else return false
if(!set.add(num))//set also has a contains method
{
System.out.println("You already entered " + num);
}
Upvotes: 6
Reputation: 1651
The easiest way would be to add an ArrayList and just to check if the list contains the random value:
Ar first you make and new ArrayList:
ArrayList<Integer> myGuesses = new ArrayList();
The second step is to add the random value to the list:
ArrayList<Integer> myGuesses = new ArrayList();
Now you only have to check if if the List cotains the value bevore generating a new one and counting your trys:
if(myGuesses.contains(MyGuess))
{
//your code
}
I applied this to your code:
Scanner scan = new Scanner(System.in); // Number to guess //
Random rand = new Random(); // Generates the guess //
int GuessNum = 0, RandGuess = 0;
ArrayList<Integer> myGuesses = new ArrayList();
System.out.println("Enter a number 1 - 100 for me to guess: ");
int input = scan.nextInt();
if (input >= 1 && input <= 100) {
int MyGuess = rand.nextInt(100) + 1;
while (MyGuess != input) {
if(myGuesses.contains(MyGuess))
{
MyGuess = rand.nextInt(100) + 1;
GuessNum++;
myGuesses.add(MyGuess);
}
}
System.out.println("I guessed the number after " + GuessNum + " tries.");
}
I hope that helps!
Upvotes: 1
Reputation: 726569
Arrays in Java are of fixed size. Once you have allocated an array, adding elements is allowed only up to the number of elements that you have allocated upfront. This wouldn't be a big issue in your situation, because the values are limited to 100, but you have better alternatives:
Java library offers collections that grow dynamically. In your case a HashSet<Integer>
would be a good choice:
HashSet
can grow to an arbitrary size as you goHashSet
for a number is a quick operationAnother solution would be to make an array of 101 boolean
s:
boolean[] seen = new boolean[101];
Now you can check if the number has been seen before by testing seen[myGuess]
to be false
, and set seen[myGuess] = true
when you see a new number. This approach is also very fast. However, you need to keep track of how many available numbers you have, because the range from 1 to 100 will get exhausted after 100 guesses, so trying to generate an additional number would become an infinite loop.
Upvotes: 3
Reputation: 22224
A Set
is an appropriate container for that functionality. You would define it like this :
Set<Integer> previous = new HashSet<>();
And to get a random number that you haven't previously tried
while (previous.contains(MyGuess))
MyGuess = rand.nextInt(100) + 1;
previous.add(MyGuess);
This will get new random numbers from rand
object until one is found that isn't in previous
. Then that is added to previous
for the next iteration.
Upvotes: 3
Reputation: 4430
Use a HashSet<Integer>
to do this. The values in a Set are unique so this is an easy way to store the already guessed values.
The contains(...)
method is how you find out if you've guessed this number before
Upvotes: 2