Results 1 to 6 of 6
  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default While statement problems

    i just can't figure this out and i butchered the code by now.....does anyone know how to resolve?

    Write a class NumberGuess whose objects can be used to play the game of guessing a secret
    number. This class should have the following two public methods, and whatever private fields
    you need to implement them: (1) void setSecretNumber(int secret) that sets the secret number
    inside the object and initializes the guess count to zero (2) void submitGuess(int guess) that is
    used to make a guess of the secret number, and outputs on the console either “Too big”, “Too
    small” or “Just right” depending on whether the stored secret number is smaller, larger or equal
    to guess. In the last case, the method should also output how many guesses (that is, calls to
    method submitGuess) it took the user to guess the secret number. These methods do not read
    any user input with Swing dialog boxes.

    i got this far....

    public class NumberGuess {

    public int secretNumber;
    public int count;

    void setSecretNumber(int secret){

    secretNumber = secret;
    count = 0;
    }
    void submitGuess(int guess)
    {
    int wrong = guess;
    while (guess != secretNumber)

    // if(guess > secretnumber) int tooBig = System.in("Your number is too big, Please enter a smaller number ");
    // else(guess < secretnumber) int toSmall = System.in("your number is too small");
    wrong = Integer.parseInt(System.out.println("please enter the correct number"));
    }
    }

  2. #2
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    i was also trying to put the if statment there but didn't work so i thought i will try to get the while statment to work for now and then work on the if statements.

  3. #3
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    ok. now the problem is incompatible types....

    public class NumberGuess {

    public int secretNumber;
    public int count;

    void setSecretNumber(int secret){

    secretNumber = secret;
    count = 0;
    }
    void submitGuess(int guess)
    {
    while (guess != secretNumber)
    {
    if(guess > secretNumber) guess = System.out.println("Your number is too big, Please enter a smaller number ");
    else if(guess < secretnumber) guess = System.out.println("your number is too small");
    }
    }
    }

  4. #4
    Alessio is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    ok, lets do your homework.

    first of all: the fields secretNumber and count should be private (thats how I read the assignment) - doesn't change much though

    setSecretNumber is fine as it is

    submitGuess:
    this method doesn't really need a while loop, it says in your assignment, that you do not need to read any values within this function, just use the parameter.
    so:
    Java Code:
    void submitGuess(int guess) {
     if(guess == secretNumber) {
       System.out.println("Just right, it took you " + count + " guesses");
     } else if ( guess < secretNumber) {
        count++;
        System.out.println("too low");
     }
       //I think you can handle it from here on
    }
    Regards,
    Alessio

  5. #5
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    ok. at work now and cannot test your method. but is your method going to keep prompting the user to keep entering a number? i don't see how?

    (btw. this is not exactly homework, it's a sample exam question lol)

    tks alot for the help

  6. #6
    Alessio is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    well, you'll need to instantiate the class

    pseudocode:

    Java Code:
    i = new NumberGuess()
    i.setSecretNumber(42)
    
    while(something) {
     i.submitGuess(someValue)
    }

Similar Threads

  1. problems with print statement
    By soccer_kid_6 in forum New To Java
    Replies: 8
    Last Post: 03-22-2010, 02:24 PM
  2. add an If Else statement and......uh????
    By sonny in forum New To Java
    Replies: 6
    Last Post: 03-04-2010, 06:57 PM
  3. if statement help please!!
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 12-02-2008, 02:56 PM
  4. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •