Monday, 15 March 2010

Java - No Duplicate number array -



Java - No Duplicate number array -

i in process of creating of lottery programme using java via bluej , having problem user inputted numbers , number beingness generated programme (up , including 1-49), need numbers entered user not duplicate i.e. user cannot come in 1 , 1.

i not sure how numbers not duplicate thinking of using array im not sure type or begin im rather new whole programming thing.

import java.util.arrays; import java.util.random; import java.util.scanner; public class javaapplication8 { public static void main(string[] args) { scanner user_input = new scanner (system.in); scanner keyin = new scanner(system.in); int[] lotterynumbers = new int[6]; int input; int count = 0; system.out.print("welcome lottery programme takes\nyour lottery numbers , compares\nthem weeks lottery numbers!"); system.out.print("\n\npress come in key continue"); keyin.nextline(); (int = 0; < lotterynumbers.length; i++) { count ++; system.out.println("enter 5 lottery numbers " + count + " (must between 1 , 49): "); input = integer.parseint(user_input.next()); if (input < 1 || input > 49) { while (input < 1 || input > 49) { system.out.println("invalid number entered! \nplease come in lottery number (between 1 , 49) " + count); input = integer.parseint(user_input.next()); if (input >= 1 || input <= 49) { lotterynumbers[i] = input; } } } else { lotterynumbers[i] = input; } } system.out.println("thank numbers.\nthe scheme check if have matching numbers"); system.out.print("press come in key continue"); keyin.nextline(); random randnumgenerator = new random(); stringbuilder output = new stringbuilder(); int[] actlotterynumbers = new int[6]; (int j = 0; j < actlotterynumbers.length; j++) { int roll = randnumgenerator.nextint(49); actlotterynumbers[j] = roll; } system.out.println(arrays.tostring(actlotterynumbers)); int counter = 0; (int = 0; < lotterynumbers.length; i++) { (int j = 0; j < actlotterynumbers.length; j++) { if (lotterynumbers[i] == actlotterynumbers [j]) { counter ++; system.out.println("the numbers match are: \n" + lotterynumbers[i]); } } } if (counter == 0) { system.out.println("you had no matching numbers week ... seek 1 time again next week!"); } } }

as "fge" mentioned, utilize set add together values getting user.

get user inputs , add together set.

use iterator check user entered values , generated random numbers.

set myset = new hashset(); myset.add(user_input1); myset.add(user_input1);

to retrive utilize iterator'

iterator iterator = myset.iterator(); while(iterator.hasnext(){ int value= iterator.next(); if(randomvalue==value) //do logic here }

java numbers bluej

No comments:

Post a Comment