gpt4 book ai didi

java - 无法获得超过一个随机数来匹配我的五个猜测

转载 作者:行者123 更新时间:2023-11-29 05:03:49 25 4
gpt4 key购买 nike

有人可以向我解释为什么我似乎无法获得多个随机数来匹配我的猜测吗?这里的目标是产生一个随机数,然后要求用户输入五个数字并查看有多少匹配。我使用了一个临时的 JOptionPane(不在下面的代码中)来显示随机数是什么,但它只显示一个随机值。问题可能出在数组中,还是仅仅是因为我生成了一个随机数?

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?");
}

}

}

最佳答案

你的类(class)有多个问题,首先你只生成一个随机数,其次你只匹配第一个答案。以下是修改后的版本

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?");
}

}

}

修改后的版本如下:

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?");
}

}

}

关于java - 无法获得超过一个随机数来匹配我的五个猜测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31090227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com