如何防止用户重复输入相同的号码

时间:2019-05-28 19:47:37

标签: java validation

我正在用Java创建一个程序,用户必须猜测计算机生成的随机数,用户总共尝试20次才能猜测该数字,如果用户反复输入相同的数字,则应显示错误消息并且不算是他们的猜测之一,但不确定如何做到这一点。例如,如果用户首先猜为5,然后又猜为5,则会出现错误消息,提示“您已经输入了这个数字

package guessinggame2;
import java.util.Scanner;

public class GuessingGame2 {

  static Scanner kboard = new Scanner(System.in);

  public static void main(String args[]) // start of main 
  {
    System.out.println("Welcome to the guessing game, the computer will 
    generate a random number that you have to guess, good luck!");

    int secret_number = 0;
    int number_of_guesses = 0;
    Scanner input = new Scanner(System.in);
    int user_guess;
    int used_number = 0;

    secret_number = (int)(Math.random()*100) + 1;
    System.out.println("The computer has generated it's number");

    for(int i=0; i<20;i++) {

      System.out.println("Please make your guess");
      user_guess = kboard.nextInt();
      number_of_guesses++;

      if (user_guess == secret_number)
      {
        System.out.println("Your guess is correct it took you " + 
        number_of_guesses + " guesses");
      }

      else if  (user_guess < secret_number)
      {
        System.out.println("Your guess is too low try again");
      }

      else if (user_guess > secret_number)
      {
        System.out.println("Your guess is too high try again");
      }

      System.out.println (20 - number_of_guesses + " Guesses remaining"); 
    }
  }
}

4 个答案:

答案 0 :(得分:1)

要研究的关键字:arrays

只需将用户的所有数字存储到这样的数组中。然后,您可以在每次给出新数字时迭代该数组。当您在该阵列中找到该编号时,可以打印错误消息。

或者,您可以使用HashSet存储以前输入的数字。集合有一个很好的方法 contains(),它避免了数组的重复循环。但是HashSet之类的集合类有点高级,对于新手来说,通常的做法是基于数组的解决方案。

答案 1 :(得分:1)

  1. 使用ArrayList创建一个数组并为其命名(例如“ entered_numbers”)
ArrayList<Integer> entered_numbers = new ArrayList<Integer>();


2.每次用户输入数字时,请使用contains()检查数字是否在ArrayList中。

如果是,则显示您的消息(不要number_of_guesses++
如果不是,请使用add()添加它,然后继续自己检查。

if(entered_numbers.contains(user_guess)) {
    System.out.println("You have already entered this number");
    continue;
} else {
    entered_numbers.add(user_guess);
    // Check if == secret number,
    // Check if < secret number,
    // Check if > secret number
}


其他人建议使用HashSets,这对性能更好。
使用ArrayList的复杂度为O(n),而HashSet为O(1)。

我选择了使用ArrayList的一种更简单的方法,它不会对性能产生太大影响,因为您不会在ArrayList中存储大量数据。

在此处查看有关复杂性的更多信息:https://www.baeldung.com/java-collections-complexity

答案 2 :(得分:0)

此解决方案仅检查彼此紧接着重复的数字。

将数字保存到缓冲区中,并检查它是否曾在循环中使用过。

user_guess = kboard.nextInt();
if(user_guess == used_number){
   //WARN USER
    System.out.println("Warning");  
} else {
    used_number = user_guess;
}

您也可以改为递增,并在3次重复后发出警告:

int used_number = 0;
int used_number_times = 0;
int limit = 3;

...

user_guess = kboard.nextInt();
if(user_guess == used_number){
    used_number_times++; 
} else {
    used_number = user_guess;
    used_number_times = 0;
}
if(used_number_times > limit){
    //WARN USER
    System.out.println("Warning");  
}

或使用HashSet:

int used_number = 0;
HashSet<Integer> set = new HashSet<>();
...


user_guess = kboard.nextInt();

if(!set.add(user_guess){
   //WARN USER
    System.out.println("Warning");  
} 

答案 3 :(得分:0)

您可以使用一组来跟踪用户提交的数字,例如

public class GuessingGame2 {

   static Scanner kboard = new Scanner(System.in);


   public static void main(String args[]) // start of main
   {

      System.out.println("Welcome to the guessing game, the computer will generate a random number that you have to guess, good luck!");

      int secret_number = 0;
      int number_of_guesses = 0;
      Scanner input = new Scanner(System.in);
      int user_guess;
      int used_number = 0;
      HashSet<Integer> user_inputs = new HashSet<>();

      secret_number = (int) (Math.random() * 100) + 1;
      System.out.println("The computer has generated it's number");

      for (int i = 0; i < 20; i++) {

         System.out.println("Please make your guess");
         user_guess = kboard.nextInt();
         if (!user_inputs.add(user_guess)) {
            System.out.println("Please provide another input");
            continue;
         }
         number_of_guesses++;

         if (user_guess == secret_number) {
            System.out.println("Your guess is correct it took you " +
                  number_of_guesses + " guesses");
         } else if (user_guess < secret_number) {
            System.out.println("Your guess is too low try again");
         } else if (user_guess > secret_number) {
            System.out.println("Your guess is too high try again");
         }

         System.out.println(20 - number_of_guesses + " Guesses remaining");


      }

   }

}