Non Duplicate number in user input [duplicate]

。_饼干妹妹 提交于 2020-04-17 22:50:39

问题


With this Lottery program I am trying to make sure the number are within a range of 1-59, The same number cannot be entered twice and that only numbers can be entered. The only bit of code I need help on is on the only numbers can be entered part.


public void choose() {
        System.out.println("\n");
        int temp;
        boolean valid;

        for (int i = 0; i < 6; i++) {
            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, please enter a valid 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); 
        }
    }

回答1:


Do it as follows:

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 = 0;
        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): ");
                try {
                    temp = Integer.parseInt(keyboard.nextLine());
                    if (temp < 1 || temp > 59) {
                        System.out.println("Error: Invalid integer.");
                        valid = false;
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Error: The input is not an 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
        }
    }
}

A sample run:

Enter in an integer (from 1 to 59): a
Error: The input is not an integer.
Enter in an integer (from 1 to 59): 12.23
Error: The input is not an integer.
Enter in an integer (from 1 to 59): 10
Enter in an integer (from 1 to 59): 200
Error: Invalid integer.
Enter in an integer (from 1 to 59): 20
Enter in an integer (from 1 to 59): 5
Enter in an integer (from 1 to 59): 10
Please enter a different number as you have already entered this
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): 6
Enter in an integer (from 1 to 59): 7
Enter in an integer (from 1 to 59): 12
[10, 20, 5, 6, 7, 12]



回答2:


Try:

System.out.println("\n");
int temp;
boolean valid;

for (int i = 0; i < 6; i++) {
    do {
        valid = true;
        System.out.print("Enter in an integer from 1 to 59: ");
        while (!keyboard.hasNextInt()) {
            keyboard.next();
            System.out.println("Error.! Please enter a number");
            System.out.print("Enter in an integer from 1 to 59: ");
        }
        temp = keyboard.nextInt();
        if (temp < 1 || temp > 59) {
            System.out.println("Error, please enter a valid 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);
}

You can check if user had entered an int or not with the method keyboard.hasNextInt().




回答3:


This May be the ULTIMATE SOLUTION but require plenty of brain power to actually understand how it works.

Ask me if you need some explaination

public class Test {


public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    ArrayList<String> numbers = new ArrayList<>(6);
    String s ;
    do {
        System.out.println("Enter a number between 1 and 59");
        s = scanner.nextLine().toString();
    }
    while(!s.matches("[0-9]|[1-5][0-9]") || numbers.contains(s) || numbers.size() < 5 && numbers.add(s));
    }
}

also tell me if you want prompts for invalid range/format and duplicate entries i shall figure out a way



来源:https://stackoverflow.com/questions/60578282/non-duplicate-number-in-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!