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 2 of 2

Thread: Guessing game help

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guessing game help

    Hi guys, so I am trying to do a program that takes a limited number of users 2-5 and then gets there guesses and records them till they select the correct one. The winner is determined by whoever makes the least amount of guesses for there random number. So far here is my work, but I am stuck on the part for getting the amount of guesses and finding out the winner. Any help?





    public static void main(String[] args) {



    System.out.println("How many players between 2-5?"); // this collects the amount of players in the game using method recNum
    int u = recNum();


    int [ ] players = new int [ u ]; //this array holds the number guessed by each player using the method get recNum

    for (int i = 0; i < players.length; i++ )
    {
    System.out.println("Please enter your guess now player" + i);
    players[i]=recNum();
    }



    int [] guesses = new int [u ]; //this array holds the random number using the method getNum

    for (int c = 0; c < guesses.length; c++)
    {
    guesses[c]=getNum();


    }

    int []z;
    boolean f = true;
    do{

    z = compare(players, guesses, u);
    } while (!f);

    winner(z);


    }


    public static void winner(int z[])
    {

    int leader = 0;
    for (int i = 0; i < z.length; i++)
    {
    if (z[i] > leader)
    {
    leader = z[i];
    }
    }
    System.out.println("The winner is player" + leader);
    }
    public static int[] compare(int players[], int guesses[], int u)
    {
    int [] x = new int[u];
    boolean zebra = true;
    while (zebra){
    for(int q = 0; q < x.length; q++)
    {

    if (players[q] < guesses[q])
    {
    System.out.println("Player" + " " + q + " your guess is too low");
    System.out.println(guesses[q]);
    x[q]++;





    players[q]=recNum();


    }
    else if (players[q] > guesses[q])
    {
    System.out.println("Player" + " " + q + " your guess is too high");
    System.out.println(guesses[q]);
    x[q]++;





    players[q]=recNum();

    }
    else
    {


    System.out.println("Player" + " " + q + " your guess is correct");
    System.out.println(guesses[q]);
    zebra = false;
    x[q]++;

    }


    }



    }

    return x;

    }




    public static int recNum()
    {
    Scanner scanz = new Scanner(System.in);
    int n1 = 0;
    boolean guess = false;
    while(!guess){
    try{
    n1 = scanz.nextInt();
    }
    catch(InputMismatchException inputMismatchException)
    {

    System.out.println("Your entry was not an integer, please try again");

    continue;
    }
    if(n1>100 || n1<1){

    System.out.println("Your guess must be between 1 and 100 please try again");

    continue;
    }
    else{
    guess = true;
    }
    }
    return n1;
    }


    public static int getNum(){ //this method goes and creates a random number from 1-100 using the imported java ulti random

    Random geney = new Random();
    int n2 = 0;
    int min = 1;
    int max = 100;
    n2 = geney.nextInt(max - min + 1) + min;
    return n2;
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Guessing game help

    When posting code, please use the highlight tags. Unformatted code is very hard to read.

    Take your problems one at a time. How do you know when a user has won? When should you check for that? How do you know when a user makes a guess? How would you keep track of that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 02:30 PM
  2. guessing game assignment
    By scottey313 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 22nd, 2011, 07:52 PM
  3. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 02:50 PM
  4. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 09:46 PM
  5. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 12:41 PM