好的,所以我需要在java中创建一个基本上是随机数生成器的程序,玩家需要猜测从1-10生成的数字。
我已经获得了代码,一切都运行得很好(代码发布在下面),但我想这样做,以便用户不能使用相同的数字两次。
比如说,例如玩家再次输入2然后是3然后是2。我想要它 返回"您已输入此号码"对于我的生活,我无法让它发挥作用。
我一直在尝试使用HashSets,因为我相当确定该方法是正确的方法,但是当我回想起HashSet中已有的数字时,我会被搞砸他们已经在HashSet中然后返回"您已经输入了这个号码"。
我试着修补
if (hs.contains(guess)) == true)
System.out.println("You have already entered this number")
我也放hs.add(guess)
并猜测玩家输入了什么号码。我真的很难过,如果你们能指导我朝着正确的方向前进,我会非常感激。
非常感谢先进的
package guessgame;
import java.util.HashSet;
import java.util.Scanner;
public class GuessGame {
public static void main(String[] args) {
HashSet<Integer>hs = new HashSet();
int GuessLogic;
GuessLogic = (int) (Math.random() * 10 + 1);
Scanner keyboard = new Scanner (System.in);
int guess;
int NumGuess;
NumGuess = 1;
do {
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
if (guess == GuessLogic)
System.out.println("You got it right!! Congrats!! Total Number of Guesses: " + NumGuess++);
else if (guess < GuessLogic && guess > 0)
System.out.println("You are wrong!!! Hint: Guess Higher, Guess number: " + NumGuess++);
else if (guess > GuessLogic && guess <= 10)
System.out.println("You are wrong!!! Hint: Guess Lower, Guess number: " + NumGuess++);
else
System.out.println("Your guess is out of the specified range. Please try again." );
} while (guess != GuessLogic);
}
}
答案 0 :(得分:3)
首先,你应该编程到界面 Set
(而不是具体的类型HashSet
)。接下来,如果此集合尚未包含指定的元素,则可以使用Set.add(E)
返回 true
。最后,我建议使用有意义的变量名。将它们放在一起,可能看起来像,
Set<Integer> hs = new HashSet<>();
int correctNumber = 1 + (int) (Math.random() * 10);
Scanner keyboard = new Scanner(System.in);
int guessCount = 1;
int guess;
do {
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
if (hs.add(guess)) {
if (guess == correctNumber) {
System.out.println("You got it right!! Congrats!! Total Number of "
+ "Guesses: " + guessCount++);
} else if (guess < correctNumber && guess > 0) {
System.out.println("You are wrong!!! Hint: Guess "
+ "Higher, Guess number: " + guessCount++);
} else if (guess > correctNumber && guess <= 10) {
System.out.println("You are wrong!!! Hint: Guess "
+ "Lower, Guess number: " + guessCount++);
} else {
System.out.println("Your guess is out of the specified range. "
+ "Please try again.");
}
} else {
System.out.println("You have already entered this number");
}
} while (guess != correctNumber);
答案 1 :(得分:2)
使用HashSet
。如果add
函数已包含该对象,则返回false:
Set<Integer> guesses = new HashSet<Integer>();
if (guesses.add(guess)) {
//Code for their new guess
} else {
System.out.println("You have already guessed this number!");
}
答案 2 :(得分:0)
这是完整版(重写了一点)。
您可能希望将NumGuess++
移动到想要增加猜测次数的位置。在猜测之后立即放置它似乎是合适的,但如果Set已包含该数字,您可能不想增加。
此外,移出范围检查可使代码更清晰。
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
HashSet<Integer> hs = new HashSet<>();
int GuessLogic = (int) (Math.random() * 10 + 1);
int guess;
int NumGuess = 0;
do {
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
NumGuess++;
if (hs.contains(guess)) {
System.out.println("You have already entered this number");
continue; // this will repeat the loop
}
if (guess < 0 || guess > 10) {
System.out.println("Your guess is out of the specified range. Please try again.");
continue; // this will repeat the loop
}
System.out.println("Your guess is " + guess);
if (guess == GuessLogic) {
System.out.println("You got it right!! Congrats!! Total Number of Guesses: " + NumGuess);
return; // this will stop the method
}
else if (guess < GuessLogic) {
System.out.println("You are wrong!!! Hint: Guess Higher, Guess number: " + NumGuess);
}
else if (guess > GuessLogic) {
System.out.println("You are wrong!!! Hint: Guess Lower, Guess number: " + NumGuess);
}
hs.add(guess);
} while (true);
}
答案 3 :(得分:0)
我使用以下代码:
package guessgame;
import java.util.ArrayList;
import java.util.Scanner;
public class GuessGame {
public static void main(String[] args) {
ArrayList<Integer> guesses = new ArrayList<>(); // Arraylist preferrable to HashSet, but not required
int GuessLogic;
GuessLogic = (int)(Math.random() * 10 + 1);
Scanner keyboard = new Scanner(System.in);
int guess;
int NumGuess;
NumGuess = 1;
do {
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
if (guesses.contains(guess)) { // If the player has already guessed the number
System.out.println("You have already entered this number");
continue; // Skips to the next loop iteration
}
guesses.add(guess); // ONLY does this if ^ evaluates to false
System.out.println("Your guess is " + guess);
if (guess == GuessLogic)
System.out.println("You got it right!! Congrats!! Total Number of Guesses: " + NumGuess++);
else if (guess < GuessLogic && guess > 0)
System.out.println("You are wrong!!! Hint: Guess Higher, Guess number: " + NumGuess++);
else if (guess > GuessLogic && guess <= 10)
System.out.println("You are wrong!!! Hint: Guess Lower, Guess number: " + NumGuess++);
else
System.out.println("Your guess is out of the specified range. Please try again." );
} while (guess != GuessLogic);
}
}
答案 4 :(得分:0)
对于如此小范围的值,您只需使用boolean
数组就可以逃脱:
int minRngVal = 1;
int maxRngVal = 10;
boolean[] guesses = new boolean[maxRngVal];
当玩家猜到一个数字时,将其发送到如下方法:
private boolean isValidGuess(int guess){
if(guesses[guess-1] == true){
//player already guessed that number!
return false;
} else{
//player hasn't guessed that number yet
guesses[guess-1] = true;
return false;
}
}
然后只需在代码中添加以下行:
if (isValidGuess(guess)) {
System.out.println("You have already entered this number");
continue; // this will repeat the loop
}