Reputation: 127
Could someone explain to me why I can't seem to get more than one random number to match my guess? The goal here is to produce one random number then ask the user to enter five numbers and see how many match. I used a temporary JOptionPane (not in the code below) to display what the random number is, but it only shows me one random value. Could the issue be in the array or is it only because I'm producing one random number?
public class Test1 {
public static void main(String[] args) {
final int MIN_NUM = 1;
final int MAX_NUM = 5;
int count = 0;
int randomNum = getRandomNum(MIN_NUM, MAX_NUM);
int guess = getUserGuess();
boolean determineMatch = isGuessCorrect(randomNum, guess);
if (determineMatch == true) {
count++;
}
displayResult(count);
}
public static int getRandomNum(int MIN_NUM, int MAX_NUM) {
return (int)(Math.random() * MAX_NUM) + MIN_NUM;
}
public static int getUserGuess() {
final int MIN_GUESS = 1;
final int MAX_GUESS = 5;
int x = 0;
int[] guess = new int[MAX_GUESS];
for (int i = 0; i < guess.length; i++) {
do {
try {
guess[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 5:"));
}
catch (NumberFormatException e) {
guess[i] = 0;
}
if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
JOptionPane.showMessageDialog(null, "ERROR! Enter a number between 1 and 5");
}
} while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);
}
return guess[x];
}
public static boolean isGuessCorrect(int randomNum, int guess) {
boolean determineMatch = true;
if (randomNum == guess) {
determineMatch = true;
}
else {
determineMatch = false;
}
return determineMatch;
}
public static void displayResult(int count) {
if (count >= 0 || count <= 1) {
JOptionPane.showMessageDialog(null,
"Out of 5 tries, you guessed " + count + " correct.\nYou don’t have any supernatural powers. Sorry!");
}
else if (count >= 2 || count <= 3) {
JOptionPane.showMessageDialog(null,
"Out of 5 tries, you guessed " + count + " correct.\nYou might be good. Try again another time.");
}
else if (count >= 4 || count <= 5) {
JOptionPane.showMessageDialog(null,
"Out of 5 tries, you guessed " + count + " correct.\nYou’re hired! When can you start?");
}
}
}
Upvotes: 1
Views: 71
Reputation: 3514
There are multiple problems with your class, first you only generating one random number, second you're only matching with first answer. below is revised version
package com.test;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class Test1 {
public static void main(String[] args) {
final int MIN_NUM = 1;
final int MAX_NUM = 5;
int count = 0;
int[] guesses = getUserGuess();
List<Integer> randomNums = getRandomNum(MIN_NUM, MAX_NUM);
for (int i = 0; i < guesses.length; i++) {
boolean determineMatch = isGuessCorrect(randomNums, guesses[i]);
if (determineMatch == true) {
count++;
}
}
displayResult(count);
}
public static List<Integer> getRandomNum(int MIN_NUM, int MAX_NUM) {
List<Integer> randomNums = new ArrayList<Integer>();
for (int i = MIN_NUM; i <= MAX_NUM; i++) {
randomNums.add((int) (Math.random() * MAX_NUM) + MIN_NUM);
}
return randomNums;
}
public static int[] getUserGuess() {
final int MIN_GUESS = 1;
final int MAX_GUESS = 5;
int[] guess = new int[MAX_GUESS];
for (int i = 0; i < guess.length; i++) {
do {
try {
guess[i] = Integer
.parseInt(JOptionPane
.showInputDialog("Enter a number between 1 and 5:"));
}
catch (NumberFormatException e) {
guess[i] = 0;
}
if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
JOptionPane.showMessageDialog(null,
"ERROR! Enter a number between 1 and 5");
}
} while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);
}
return guess;
}
public static boolean isGuessCorrect(List<Integer> randomNums, int guess) {
boolean determineMatch = true;
if (randomNums.contains(guess)) {
determineMatch = true;
} else {
determineMatch = false;
}
return determineMatch;
}
public static void displayResult(int count) {
if (count >= 0 || count <= 1) {
JOptionPane
.showMessageDialog(
null,
"Out of 5 tries, you guessed "
+ count
+ " correct.\nYou don’t have any supernatural powers. Sorry!");
} else if (count >= 2 || count <= 3) {
JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
+ count
+ " correct.\nYou might be good. Try again another time.");
} else if (count >= 4 || count <= 5) {
JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
+ count + " correct.\nYou’re hired! When can you start?");
}
}
}
Below is the revised version:
package com.test;
import javax.swing.JOptionPane;
public class Test1 {
public static void main(String[] args) {
final int MIN_NUM = 1;
final int MAX_NUM = 5;
int count = 0;
int[] guesses = getUserGuess();
int randomNum = getRandomNum(MIN_NUM, MAX_NUM);
for (int i = 0; i < guesses.length; i++) {
boolean determineMatch = isGuessCorrect(randomNum, guesses[i]);
if (determineMatch == true) {
count++;
}
}
displayResult(count);
}
public static int getRandomNum(int MIN_NUM, int MAX_NUM) {
return (int) (Math.random() * MAX_NUM) + MIN_NUM;
}
public static int[] getUserGuess() {
final int MIN_GUESS = 1;
final int MAX_GUESS = 5;
int[] guess = new int[MAX_GUESS];
for (int i = 0; i < guess.length; i++) {
do {
try {
guess[i] = Integer
.parseInt(JOptionPane
.showInputDialog("Enter a number between 1 and 5:"));
}
catch (NumberFormatException e) {
guess[i] = 0;
}
if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
JOptionPane.showMessageDialog(null,
"ERROR! Enter a number between 1 and 5");
}
} while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);
}
return guess;
}
public static boolean isGuessCorrect(int randomNum, int guess) {
boolean determineMatch = true;
if (randomNum == guess) {
determineMatch = true;
} else {
determineMatch = false;
}
return determineMatch;
}
public static void displayResult(int count) {
if (count >= 0 || count <= 1) {
JOptionPane
.showMessageDialog(
null,
"Out of 5 tries, you guessed "
+ count
+ " correct.\nYou don’t have any supernatural powers. Sorry!");
} else if (count >= 2 || count <= 3) {
JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
+ count
+ " correct.\nYou might be good. Try again another time.");
} else if (count >= 4 || count <= 5) {
JOptionPane.showMessageDialog(null, "Out of 5 tries, you guessed "
+ count + " correct.\nYou’re hired! When can you start?");
}
}
}
Upvotes: 2
Reputation: 751
There are some errors in this function.
public static int getUserGuess() {
final int MIN_GUESS = 1;
final int MAX_GUESS = 5;
int x = 0; // <-- Problem with this variable. Trace the value of 'x' through this function - you are always returning guess[x] and x is always 0
int[] guess = new int[MAX_GUESS];
for (int i = 0; i < guess.length; i++) {
do {
try {
guess[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 5:"));
}
catch (NumberFormatException e) {
guess[i] = 0;
}
if (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS) {
JOptionPane.showMessageDialog(null, "ERROR! Enter a number between 1 and 5");
}
} while (guess[i] < MIN_GUESS || guess[i] > MAX_GUESS);
}
return guess[x]; // This function returns a SINGLE integer rather than the entire array of inputs, so the call to isGuessCorrect() only checks one guess.
}
Consider changing the return type of getUserGuess to an array of integers:
public static int[] getUserGuesses() {
final int MIN_GUESS = 1;
final int MAX_GUESS = 5;
int[] guesses = new int[MAX_GUESS];
for (int i = 0; i < guesses.length; i++) {
do {
try {
guesses[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 5:"));
}
catch (NumberFormatException e) {
guesses[i] = 0;
}
if (guesses[i] < MIN_GUESS || guesses[i] > MAX_GUESS) {
JOptionPane.showMessageDialog(null, "ERROR! Enter a number between 1 and 5");
}
} while (guesses[i] < MIN_GUESS || guesses[i] > MAX_GUESS);
}
return guesses;
}
In your main method, since we are now returning an array of integers from getUserGuesses(), we store the data as an array of integers.
int[] guesses = getUserGuesses();
Now with your array of guesses being in a more flexible scope of your program, you can decide how you would like to perform your logic. Going for something that you originally tried to implement, it would look something like this:
int[] guesses = getUserGuesses();
for (int i=0; i<guesses.length; i++)
if (isGuessCorrect(randomNum, guesses[i]))
count++;
Also -- in just glancing over some of the other functions.. there appears to be some logic errors.
displayResult(5);
prints:
Out of 5 tries, you guessed 5 correct. You don't have any supernatural powers. Sorry!
This can be fixed by changing the OR statements to AND statements:
public static void displayResult(int count) {
count = 5;
if (count >= 0 && count <= 1) {
JOptionPane.showMessageDialog(null,
"Out of 5 tries, you guessed " + count + " correct.\nYou don’t have any supernatural powers. Sorry!");
}
else if (count >= 2 && count <= 3) {
JOptionPane.showMessageDialog(null,
"Out of 5 tries, you guessed " + count + " correct.\nYou might be good. Try again another time.");
}
else if (count >= 4 && count <= 5) {
JOptionPane.showMessageDialog(null,
"Out of 5 tries, you guessed " + count + " correct.\nYou’re hired! When can you start?");
}
}
Upvotes: 1
Reputation: 11
public static void main(String[] args) {
final int MIN_NUM = 1;
final int MAX_NUM = 5;
int count = 0; //count is set to 0
int randomNum = getRandomNum(MIN_NUM, MAX_NUM);
int guess = getUserGuess();
boolean determineMatch = isGuessCorrect(randomNum, guess);
if (determineMatch == true) {
count++; //count set to 1
}
displayResult(count); //every time displayResult is called with count = 1
}
Your count variable is only incremented once to a value of 1, so your displayResult() every time displays that you only got one guessed right
Upvotes: 0