Checking for user input to be only integers in Java

大憨熊 提交于 2020-01-05 06:26:07

问题


I'm doing lottery game for my assignment (user inputs 6 number, i will generate 8 unique winning numbers and 2 last numbers are supplementary). I need help with user input checking if input numbers are from 1 to 45 and input must be int, when input is not integer it throws an error.

This programming way is procedure way, how can i change it into object oriented way? I know that I must make methods in another java file and then link it back to this main. Can you suggest me how to do it?

I have tried try and catch, if and else (for input check) but i don't know how to check user input when it's in array. Thank you for help.

Here is my code:

class Lottery {

public static void main ( String[] args ) {

    System.out.println("\nWelcome to the Lottery game.");
    System.out.println("You can enter numbers from 1 to 45.");

    // User input into an array
    int[] input = new int[6];

    Scanner scanner = new Scanner(System.in);

    System.out.println("\nPlease enter your 6 lucky numbers: ");
    for(int j = 0; j < 6; j++) {

        input[j]=scanner.nextInt();
    }

    int check = scanner.nextInt();
    if(check < 0 && check > 45) {
        System.out.println("\nERROR: Please enter only numbers from 1 to 45!");
    }

    // Printing out unique winning numbers from random generator
    System.out.println("\nWinning numbers: ");
    MultiRandomGenerator mrg = new MultiRandomGenerator();
    int[] set;

    set = mrg.getSet();
    for (int i = 0; i < set.length; i++) {

        System.out.print(set[i] + " ");
    }

    // Loops for counting how many numbers user has guessed right
    int count = 0; // for 6 numbers
    int scount = 0; // for 2 last supplementary numbers

    for(int i = 0; i < input.length; i++) {

        for(int k = 0; k < set.length; k++) {

            if (k < 6) {

                if (set[k] == input[i]) {

                    count++;
                } else {
                    if (set[k] == input[i]) {

                    scount++;
                    }

                }

            }

        }
    }
    System.out.print("\n\nYou guessed right " + count + " winning numbers.");
    System.out.print("\nYou guessed right " + scount + " suplementary numbers.");

    // If statments for printing out winning prizes
    if (count == 6) {

        System.out.println("\nYou have won 1st price!");
    } if (count == 5 && scount == 1) {

        System.out.println("\nYou have won 2st price!");
    } if (count == 5) {

        System.out.println("\nYou have won 3st price!");
    } if (count == 4) {

        System.out.println("\nYou have won 4st price!");
    } if (count == 3 && scount == 1) {

        System.out.println("\nYou have won 5st price!");
    } if (count == 1 && scount == 2) {

        System.out.println("\nYou have won 6st price!");
    } else {

        System.out.println("\nSorry, you didn't won anything.");
    }
}
}

回答1:


Sample code to go through array and find the invalid user input.

set = mrg.getSet();
String[] userDataStatus = new String[45];
for (int i = 0; i < set.length; i++) 
{
    try
    {
        String inputdata = set.get(i);
        if(inputdata != null && inputdata.trim().length() > 0)
        {
            int currentNumber = Integer.parseInt(userdata);
            userDataStatus[i] = "Y";//Y represent valid number
        }
    }
    catch (NumberFormatException ex )
    {
        userDataStatus[i] = "N";//If it throws exception then save as 'N'       
    }
}

Use the above String array and display error messaage to users.




回答2:


You can check in your loop, something like

int val;
try
{
   input[j] = Integer.parseInt( scanner.nextString() );
}
catch (NumberFormatException ex )
{

}


来源:https://stackoverflow.com/questions/15960434/checking-for-user-input-to-be-only-integers-in-java

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