Checking for user input to be only integers in Java

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.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

以上就是Checking for user input to be only integers in Java的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准