用户输入中的数字不重复



我正在尝试创建一个输入验证,它不会让你输入同一个数字两次,也不会让你在一个数字范围内,除非它是整数,否则什么都不能输入。我目前正在创建一个彩票计划,我不确定如何做到这一点。任何帮助都将不胜感激。我的数字范围验证有效,但其他两个验证无效。我尝试了不重复的数字验证,但我不确定如何进行仅数字验证。有人能告诉我如何构造这个吗。

这个方法在我的播放器类中

public void choose() {
        int temp = 0;
        for (int i = 0; i<6; i++) {
            System.out.println("Enter enter a number between 1 & 59");
            temp = keyboard.nextInt();
            keyboard.nextLine();
            while ((temp<1) || (temp>59)) {
                System.out.println("You entered an invalid number, please enter a number between 1 and 59");
                temp = keyboard.nextInt();
                keyboard.nextLine();
            }
            if (i > 0) {
                while(temp == numbers[i-1]) {
                    System.out.println("Please enter a different number as you have already entered this");
                    temp = keyboard.nextInt();
                    keyboard.nextLine();
                }
            }
            numbers[i] = temp;
        }
    }

按如下操作:

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    static int[] numbers = new int[6];
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String args[]) {
        // Test
        choose();
        System.out.println(Arrays.toString(numbers));
    }
    static void choose() {
        int temp;
        boolean valid;
        for (int i = 0; i < 6; i++) {
            // Check if the integer is in the range of 1 to 59
            do {
                valid = true;
                System.out.print("Enter in an integer (from 1 to 59): ");
                temp = keyboard.nextInt();
                if (temp < 1 || temp > 59) {
                    System.out.println("Error: Invalid integer.");
                    valid = false;
                }
                for (int j = 0; j < i; j++) {
                    if (numbers[j] == temp) {
                        System.out.println("Please enter a different number as you have already entered this");
                        valid = false;
                        break;
                    }
                }
                numbers[i] = temp;
            } while (!valid); // Loop back if the integer is not in the range of 1 to 100
        }
    }
}

样本运行:

Enter in an integer (from 1 to 59): 100
Error: Invalid integer.
Enter in an integer (from 1 to 59): -1
Error: Invalid integer.
Enter in an integer (from 1 to 59): 20
Enter in an integer (from 1 to 59): 0
Error: Invalid integer.
Enter in an integer (from 1 to 59): 4
Enter in an integer (from 1 to 59): 5
Enter in an integer (from 1 to 59): 20
Please enter a different number as you have already entered this
Enter in an integer (from 1 to 59): 25
Enter in an integer (from 1 to 59): 6
Enter in an integer (from 1 to 59): 23
[20, 4, 5, 25, 6, 23]

对于测试数字数组中存在的值,请使用Arrays.asList(numbers).contains(temp)

如果您使用ArrayList来存储数字,可能会更好。

我会递归重写该方法以避免多个循环。

如果您不熟悉递归方法,那么它基本上是一个在方法内部调用自己的方法。通过使用巧妙的参数,可以将递归方法用作循环。例如

void loop(int index) {
   if(index == 10) {
       return;   //End loop
   } 
   System.out.println(index);
   loop(index++);
}

通过呼叫CCD_ 2,将打印数字1至9。

在您的情况下,递归方法可能看起来像

public void choose(int nbrOfchoices, List<Integer> taken) {
      if(nbrOfChoices < 0) {
          return; //Terminate the recursively loop
      }
      System.out.println("Enter enter a number between 1 and 59");
      try {      
          int temp = keyboard.nextInt(); //Scanner.nextInt throws InputMismatchException if the next token does not matches the Integer regular expression
      } catch(InputMismatchException e) {
          System.out.println("You need to enter an integer");
          choose(nbrOfChoices, taken);
          return;
      }
      if (value < 1 || value >= 59) { //Number not in interval
            System.out.println("The number " + temp + " is not between 1 and 59.");
            choose(nbrOfChoices, taken);
            return;
        }
        if (taken.contains(temp)) { //Number already taken
            System.out.println("The number " + temp + " has already been entered.");
            choose(nbrOfChoices, taken);
            return;
        }
        taken.add(temp);
        choose(nbrOfChoices--, taken);
}

现在,您可以通过调用select(您的NumberOfchoices,您的ArrayList take(来递归启动该方法。如果你想改变你的数字间隔,你也可以很容易地添加两个附加参数。

您想要做的是使用递归,这样您就可以要求他们再次提供输入。您可以定义选项而不是6。您可以将maxExclusive定义为59(在本例中为60(。由于集只能包含唯一的非null值,因此可以跟踪选定的整数值集。在每次select调用结束时,我们再次调用select,只剩下1个选项,而不是for循环。在每个方法调用开始时,我们检查选项是否<0,如果是,我们将阻止执行。

    public void choose(Scanner keyboard, int choices, int maxExclusive, Set<Integer> chosen) {
        if (choices <= 0) {
            return;
        }
        System.out.println("Enter enter a number between 1 & " + (maxExclusive - 1));
        int value = keyboard.nextInt();
        keyboard.nextLine();
        if (value < 1 || value >= maxExclusive) {
            System.out.println("You entered an invalid number.");
            choose(keyboard, choices, maxExclusive, chosen);
            return;
        }
        if (chosen.contains(value)) {
            System.out.println("You already entered this number.");
            choose(keyboard, choices, maxExclusive, chosen);
            return;
        }
        chosen.add(value);
        choose(keyboard, --choices, maxExclusive, chosen);
    }
choose(new Scanner(System.in), 6, 60, new HashSet<>());

我希望这会有所帮助,如果是,请投票支持

import java.util.ArrayList;
import java.util.Scanner;
public class Test {
private ArrayList<String> choose() {
Scanner scanner = new Scanner(System.in);
ArrayList<String> alreadyEntered = new ArrayList<>(6); // using six because your loop indicated me that you're taking six digits 
for(int i = 0 ; i < 6 ; ++i){ // ++i is more efficient than i++
    System.out.println("Enter a number between 1 & 59");
    String digit;
    digit = scanner.nextLine().trim();
    if(digit.matches("[1-5][0-9]|[0-9]" && !alreadyEntered.contains(digit))// it checks if it is a number as well as if it is in range as well if it is not already entered, to understand this learn about regular expressions
    alreadyEntered.add(digit);
    else {
      System.out.println("Invalid input, try again");
      --i;
    }
 }
 return alreadyEntered // return or do whatever with the numbers as i am using return type in method definition i am returning
}
 scanner.close();
 } 

使用字符串的arraylist只是为了让事情变得简单,否则我将不得不从整数到字符串和字符串到整数进行一些解析

相关内容

  • 没有找到相关文章

最新更新