Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Creation of lotto type program

  1. #1
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creation of lotto type program

    alright, this is the last bit of help i need for the program, we have been asked to create a lotto type program, since i finished early i've decided to add some error checkers. one being that you cannot enter the same number twice.

    package lotto;
    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            int totalcount = 0;
            say("Welcome to The lotto!");
            int[] userinput;// declaring a new array of chars
            userinput = new int[6];//allocating 6 slots for memory
        [COLOR="Red"]    for (int array = 0; array < 6; ++array) {
                userinput[array] = finduserinput();
            }[/COLOR]
            int[] Computernumber;
            Computernumber = new int[6];
            for (int array2 = 0; array2 < 6; ++array2) {
                int tempstore = findcomputernumber();
                for(int loop = 0; loop < 6;++loop){
                   if(tempstore == Computernumber[loop] ) {
                       tempstore = findcomputernumber();
                   }
                   else{
     
                   }
                }
                Computernumber[array2] = tempstore;
                say("Random number is : " +Computernumber[array2]);
     
            }
            for (int count = 0; count < 6; ++count){
                for(int count2 = 0; count2 < 6; ++count2){
                    if(userinput[count] == Computernumber[count2]){
                        totalcount = (totalcount + 1);
                    }
                }
            }
            say("you had "+totalcount+" matches");
    if(totalcount < 3){
        say("You have won nothing.");
    }if(totalcount > 4){
        say("You have won one million dollars!");
    }else{
                int randomwin = findrandomwin();
    }
     
        }
        public static void say(String aMessage) {
            System.out.println(aMessage);
        }
        [COLOR="Red"]public static int finduserinput() {
            int name = 1;
            say("please choose a number between 0 and 40");
            try {
                Scanner scan1 = new Scanner(System.in);
                name = scan1.nextInt();
            } catch (InputMismatchException ime) {
                say("There was an error in your typing please make sure the value entered is within the set parametres");
                finduserinput();
            }
            if (name > -1 && name < 41) {
            } else {
                say("That was not a valid number please check your number and try again");
                finduserinput();
            }
            return name;
        }[/COLOR]
        public static int findcomputernumber() {
            Random Rndnumber = new Random();
            int RandomNum = Rndnumber.nextInt(41);
            return RandomNum;
        }
        public static int findrandomwin(){
            Random Rnd = new Random();
            int Randomwin = Rnd.nextInt(101);
            return Randomwin;
        }
    }
    the area's highlighted are the area's where the code will be inserted i think.. i'm not asking for the exact code needed, but rather an example or hint as to what i will need to do.

    cheers,
    luke.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: last one

    Hello luke,

    You are going to need an if statement to compare the value of the userinput array with the inputted name value.

    This is how I would probably do it. I have made my code changes bold.

    import java.util.*;
     
    public class Main {
     
       [B] public static int[] userinput = new int[6];[/B]
     
        public static void main(String[] args) {
     
           [B] Arrays.fill(userinput, -1);[/B]
     
            int totalcount = 0;
            say("Welcome to The lotto!");
     
            for (int array = 0; array < 6; ++array) {
     
                userinput[array] = finduserinput();
     
            }
     
            int[] Computernumber;
            Computernumber = new int[6];
            for (int array2 = 0; array2 < 6; ++array2) {
                int tempstore = findcomputernumber();
                for (int loop = 0; loop < 6; ++loop) {
                    if (tempstore == Computernumber[loop]) {
                        tempstore = findcomputernumber();
                    } else {
     
                    }
                }
                Computernumber[array2] = tempstore;
                say("Random number is : " + Computernumber[array2]);
     
            }
            for (int count = 0; count < 6; ++count) {
                for (int count2 = 0; count2 < 6; ++count2) {
                    if (userinput[count] == Computernumber[count2]) {
                        totalcount = (totalcount + 1);
                    }
                }
            }
            say("you had " + totalcount + " matches");
            if (totalcount < 3) {
                say("You have won nothing.");
            }
            if (totalcount > 4) {
                say("You have won one million dollars!");
            } else {
                int randomwin = findrandomwin();
            }
     
        }
     
        public static void say(String aMessage) {
            System.out.println(aMessage);
        }
     
        public static int finduserinput() {
            [B]int name = -1;[/B]
            say("please choose a number between 0 and 40");
            try {
                Scanner scan1 = new Scanner(System.in);
                name = scan1.nextInt();
            } catch (InputMismatchException ime) {
                say("There was an error in your typing please make sure the value entered is within the set parametres");
                finduserinput();
            }
            if (name > -1 && name < 41) {
     
                [B]for (int array = 0; array < 6; ++array) {[/B]
     
                    [B]if(userinput[array] == name){[/B]
                      [B]  System.out.println("Duplicate number! You have entered " + name + " twice.");
                        finduserinput();
                        }[/B]
               [B] }[/B]
     
            } else {
                say("That was not a valid number please check your number and try again");
                finduserinput();
            }
            return name;
        }
     
        public static int findcomputernumber() {
            Random Rndnumber = new Random();
            int RandomNum = Rndnumber.nextInt(41);
            return RandomNum;
        }
     
        public static int findrandomwin() {
            Random Rnd = new Random();
            int Randomwin = Rnd.nextInt(101);
            return Randomwin;
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: last one

    hey mate, i think i see what you're doing there. could you please explain this to me; Arrays.fill(userinput, -1);
    thanks.
    cheers,
    luke.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: last one

    Quote Originally Posted by luke View Post
    hey mate, i think i see what you're doing there. could you please explain this to me; Arrays.fill(userinput, -1);
    thanks.
    cheers,
    luke.
    I have used this because the default array values are 0. When we loop through the array to check for duplicate entries, because 0 already exists, the message is printed asking you to enter another unique value. By setting all the array values to -1 first, this stops that from happening. If the range was 1 to 40 rather than 0 to 40 then we wouldn't need to do this. I hope you understand
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: last one

    understand perfectly, i will not mark this thread as solved though as i plan to ask another question on it later tonight.

    cheers,
    luke.

  6. #6
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: last one

    alright, very last bit of it.

    package lotto;

    import java.util.*;

    public class Main {

    public static int[] userinput = new int[6];// declaring a new array of chars
    public static int[] Computernumber = new int[6];

    public static void main(String[] args) {
    int money = 40;
    int totalcount = 0;
    int repeat = 0;
    for (; repeat == 0 {
    Arrays.fill(userinput, 0);
    Arrays.fill(Computernumber, 0);
    say("Welcome to The lotto! each game will cost you 5 fun coupons");


    for (int array = 0; array < 6; ++array) {
    userinput[array] = finduserinput();


    }

    for (int array2 = 0; array2 < 6; ++array2) {
    Computernumber[array2] = findcomputernumber();
    say("Random number is : " + Computernumber[array2]);

    }
    for (int count = 0; count < 6; ++count) {
    for (int count2 = 0; count2 < 6; ++count2) {
    if (userinput[count] == Computernumber[count2]) {
    totalcount = (totalcount + 1);
    }
    }
    }
    say("you had " + totalcount + " matches");
    if (totalcount < 3) {
    say("You have won nothing.");
    }
    if (totalcount > 4) {
    say("You have won one million dollars!");
    money = (money + 1000000);
    } else {
    int randomwin = findrandomwin();
    money = (money + randomwin);
    }
    repeat = askrepeat();
    }
    }

    public static void say(String aMessage) {
    System.out.println(aMessage);
    }

    public static int finduserinput() {
    int name = 1;
    say("please choose a number between 1 and 40");
    try {
    Scanner scan1 = new Scanner(System.in);
    name = scan1.nextInt();
    } catch (InputMismatchException ime) {
    say("There was an error in your typing please make sure the value entered is within the set parametres");
    finduserinput();
    }
    if (name > 0 && name < 41) {
    } else {
    say("That was not a valid number please check your number and try again");
    finduserinput();
    }
    for (int array = 0; array < 6; ++array) {
    if (userinput[array] == name) {
    say("You cannot repeat your number input please enter another number.");
    finduserinput();
    } else {
    }
    }
    return name;
    }

    public static int findcomputernumber() {
    Random Rndnumber = new Random();
    int RandomNum = Rndnumber.nextInt(7);
    for (int array = 0; array < 6; ++array){
    if(RandomNum == Computernumber[array]){


    findcomputernumber();
    }

    }
    return RandomNum;
    }


    public static int findrandomwin() {
    Random Rnd = new Random();
    int Randomwin = Rnd.nextInt(101);
    return Randomwin;
    }

    public static int askrepeat() {
    say("you have " + "Fun coupons left would you like to play again?");
    int answer = 0;
    try {
    answer = System.in.read();
    System.in.read();
    } catch (Exception e) {
    say("Error in input");
    askrepeat();

    }
    if (answer == 89 || answer == 121) {
    answer = 0;
    } else if (answer == 78 || answer == 110) {
    answer = 1;
    }
    return answer;
    }
    }
    i tried to replicate what you have done, in making sure that the same number is not generated by the computer, but it seemed to fail. i still got this output.
    Random number is : 0
    Random number is : 2
    Random number is : 5
    Random number is : 3
    Random number is : 3
    Random number is : 3
    any help??


    cheers,
    luke.