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: I need some help (I'm a beginner)

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

    Default I need some help (I'm a beginner)

    I have a project that I am working on. It is a game where the computer generates a random number between 1-100 and you get 6 oppurtunities to guess the correct answer. Everything seems to run just fine. My only issue now is that the program is not displaying the correct if statements. I do not know if my if else statements are wrong or if the computer is choosing a new random number each time. I tried to test this new random number generation by printing and displaying "r" which you'll see in the code. Can anyone point me into the proper direction or give me any help. I think I am 95% done this thing after this. Thanks alot! (btw I have my print out of "r" as a comment right now) - for future reference how could i test this properly? Attached is a screenshot of the compiler. The random number is getting printed after "Please guess a number"

     
    import java.util.*;
     
    public class project2 {
      public static void main(String[] args){
      Scanner in = new Scanner (System.in);
      System.out.println("Welcome, I am thinking of a number between 1-100. Take a guess!");
      System.out.print("Please enter your number: ");
      Random r = new Random(101);
      int actual = r.nextInt();
      //System.out.print(r);
      int totalcount = 6;
      int gamecounter = 0;
        while ( gamecounter != totalcount) {
      int counter = 0;
      int turncount = 1;
      int guess = in.nextInt();
      while (counter != turncount) {
        if ( guess < actual) {
          System.out.println("You guessed " + guess);
          System.out.println("Too Low");}
        if ( guess > actual){
          System.out.println("You guessed " + guess);
          System.out.println("Too High");}
        else {
          System.out.println("You guessed " + guess);
          System.out.println("YOU'RE PSYCHIC!");}
        counter++;
      }
      gamecounter++;                 
      }
        System.out.println("Game Over Please Try Again");
    }
    }
    Attached Images Attached Images
    Last edited by Pmike86; March 1st, 2013 at 08:23 PM. Reason: formatting changes


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need some help (I'm a beginner)

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    Copy the contents of the console window from when the program is executed and paste it here. Add some comments to it explaining what the problem is.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    print out of "r"
    r is a Random class object. I don't know what use it would be. Won't the value returned by nextInt() be more useful?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. beginner
    By reginakanje in forum Object Oriented Programming
    Replies: 2
    Last Post: January 12th, 2013, 06:09 AM
  2. Beginner help please.
    By xdorkiee in forum What's Wrong With My Code?
    Replies: 59
    Last Post: December 20th, 2012, 09:19 PM
  3. hello... i am a beginner...
    By learningjava1 in forum Member Introductions
    Replies: 1
    Last Post: November 24th, 2012, 12:19 PM
  4. Beginner
    By mkarthik90 in forum Member Introductions
    Replies: 1
    Last Post: February 18th, 2012, 02:26 PM
  5. I am a beginner please help :)
    By mvonb17 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 6th, 2012, 03:44 PM

Tags for this Thread