alright well i have already posted in the python forum part, now this question is the same one as the one i did for college level computer science but this is for university. any way im pretty sure i have it but before when it was working it would say that my first guess was closer but even though the computers randomly generated number was 87 it would say my first guess 12 was closer to my guess, but this is not right and i added a bit more to it to make it look nicer but after that it would stop at the if statements and would not do them, so im wondering if someone could just have a look at it and tell me what im doing wrong and hint me as to what i should do,

guess my number question:

import java.util.Scanner;
public class RandomGuess {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int randomNumber = (int) (Math.random() * 100 + 1);
        System.out.println("I have chosen a number between 1 and 100. Try to guess it.");
        System.out.println("Please enter in your first guess:");
        int guess1 = sc.nextInt();
        System.out.println("Please enter in your second guess:");
        int guess2 = sc.nextInt();
        System.out.println("Guess#1:" + guess1);
        System.out.println("Guess#2:" + guess2);
        if (guess1 == randomNumber) {
            System.out.println("Good job! Your first guess:" + guess1 + " was exact to my number!");
        } else if (guess2 == randomNumber) {
            System.out.println("Good job! Your first guess:" + guess2 + " was exact to my number!");
        } else if (guess1 > randomNumber && guess1 > guess2) {
            System.out.println("The number was: " + randomNumber + " ." + " Your first guess was closer.");
        } else if (guess1 < randomNumber && guess1 < guess2) {
            System.out.println("The number was: " + randomNumber + " ." + " Your first guess was closer.");
        } else if (guess2 > randomNumber && guess2 > guess1) {
            System.out.println("The number was: " + randomNumber + " ." + " Your second guess was closer.");
        } else if (guess1 < randomNumber && guess1 < guess2) {
            System.out.println("The number was: " + randomNumber + " ." + " Your second guess was closer.");
        }
    }
}

im not sure what is worng so like i said, if someone or people just look it over, tell me what im doing wrong and hint me to what i should do.

thanks in advance,

strong guy :)

Your logic of condition is not correct. You are checking the different, not the comparison. It should be...

else if (guess2!=randomNumber &&
         (Math.abs(guess2-randomNumber)<Math.abs(guess1-randomNumber))) {
  System.out.println("The number was: "+randomNumber + " ." + " Your second guess was closer.");
}

However, the design of the program is too much hard-coded. Please reconsider and redesign the logic. :)

Well, first of all I dont know if the conditions uve checked for will work correctly because.....

consider number = 12, guess 1 =99 and guess 2 = 50.

Now, according to the 3rd elseif gets satisfied because, 99>12 && 99>50, and it prints guess 1 is closer but actually guess two is the closer one.

So u have to use some other technique... I would suggest comparing the differences between the guess and the number... that would be a much cleaner solution...

something like

if( abs(guess1-number)<abs(guess2-number)) guess1 is correct
else guess2 is correct

good luck!


EDIT: @Taywin sorry!!!! ur post came in when I was writing mine...didnt see it..

Your logic of condition is not correct. You are checking the different, not the comparison. It should be...

else if (guess2!=randomNumber &&
         (Math.abs(guess2-randomNumber)<Math.abs(guess1-randomNumber))) {
  System.out.println("The number was: "+randomNumber + " ." + " Your second guess was closer.");
}

However, the design of the program is too much hard-coded. Please reconsider and redesign the logic. :)

k i haven't had a chance yet to try this since i wasn't able to before install netbeans on my computer (if you could send me a pm on what i should install to get ther version i need for school, i will find out, but if just which program(s) i need to install first to get netbeasns to work then this would be very much appreciated) and im not sure what you mean the program is to much "hard-coded", would you care to explain this a bit more since i have only been at this for a few months in school now, and absolute value, also for this i don't know what this is, does, could you explain what it does and give a few examples showing how it works, and im not sure how i would redesign the logic, could you also give an example on how you would go about doing this yourself to give me an idea how i would do this? sorry im kind of a noob at this as like i mentioned before that i have only been at this for a couple of months at school. any help for me is appreciated and some things i haven't learned yet.

thank you.

Well, he has given the most important part of the solution for you.

There are so many questions at once. :P

First, Netbean is a tool which is used to help you in debugging and coding. However, it seems that many people who use Netbean does not maximize its usefulness but rather rely on the tool. This would become a big problem in the future because they do not know what to do without it. Anyway, you could try to download & install it from Oracle website http://www.oracle.com/technetwork/java/javase/downloads/index.html. The Java you need is actually from Java JDK, so you must download Java JDK regardless.

When I am talking about "hard-coded" it means that your logic expect for exact situation to be happened. In certain circumstance, hard-coded is OK to do. However, it will reduce the flexibility of what your program can do because your program likely cover only a few parts of the whole problem.

In my example of the condition testing, I show you a different approach of how to check the condition that already covers almost 1/3 of what you need to do. Let see what the problem is here. The problem is to have an unknown number generated by the program. Then 2 input numbers are in. Your program then checks how close these numbers are to the unknown.

Now, your logic is to check each condition of what would happen.

1)if (guess1 == randomNumber) { }
  This is OK because you are testing whether the guess1 is a correct answer.

2)else if (guess2 == randomNumber) { }
  This is again OK but is not really completed. What if a user enters 2 of the same number for guess1 and guess2? What kind of message you would display? Instead of using "else if" you may need to use "if" here. Or better yet, you separate the case when there is at least one correct answer and the other which is no correct answer at all.

3)else if (guess1 > randomNumber && guess1 > guess2) { }
  else if (guess1 < randomNumber && guess1 < guess2) { }
  Now, you think only the half part of if a number is greater than the unknown. Why do you have to
  break it down to bigger or smaller number? If the guess number is not correct, it is not the same
  as the unknown. That's why what you need to check is actually not equal (!=) instead of greater than
  or less than.

  Also, if the guess1 is greater than guess2, what is the meaning of comparing these numbers? Do they really
  relate to the unknown number you are checking?

  What you are looking for is the different of value between the unknown and these 2 numbers. Also,
  the different values are always positive. You do not care whether or not the the value is bigger
  or smaller.
    i.e.
    22---------55------------94
  guess1    unknown       guess2
  Which number is closer to the unknown and why? Of course, it is guess1. Why? Because the different
  between the value of guess1 and the unknown (33) is smaller than the different between guess2 and
  the unknown (39). So what does it do good to you if you compare guess1 and guess2? Also, if you
  use the unknown to subtract from guess1, you would get a positive value; where as, you use the
  unknown to subtract from guess2, you would get a negative value. However, if I swap guess1 and guess2,
  you will get the opposite. So, how would you compare the difference? In maths, you use absolute value
  for determination because you do not care for the sign. The method Math.abs() is the same as you
  converting a value to a positive regardless it is a positive value in the first place. The returned
  value is always positive with the method. It is in the Java standard library that you will need to
  memorize.

4)else if (guess2 > randomNumber && guess2 > guess1) { }
  else if (guess2 < randomNumber && guess1 < guess2) { }
  This is exactly the same as #3 above but switch guess1 and guess2.

Now, there is one thing you need to check in the condition. When a user enter 2 different numbers but both number have exactly the same distance (i.e. guess1-20, unknown-30, guess2-40), you will have to display a proper message. This is currently not in the example I showed above but should be in one of your condition check as well.

Thank you Taywin for explaining a bit more in detail about this, now i understand a bit more. now for saying that if guess 1 and guess 2 equal each other would it look like this:

if (guess1 == randomNumber) {
    System.out.println("Good job! Your first guess:" + guess1 + " was exact to my number!");
        } else if (guess2 == randomNumber){
            if (guess2 == guess1)
                System.out.println("Please do not enter two of the same number.");
                System.otu.println("Please enter your second guess again:");
                int guess3 = sc.nextInt();
            System.out.println("Good job! Your first guess:" + guess2 + " was exact to my number!");

like this is telling the computer that to print not to enter in two numbers that are the same but if saying not to enter two of the same number should only be on guess 2.

im trying right now, but having tough time (didnt sleep night before well, but still trying to figure this out without just getting answers from others).

but i think i just got what you were trying to help me with!
is this it:

/**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
        int randomNumber = (int) (Math.random() * 100 + 1);
        System.out.println("I have chosen a number between 1 and 100. Try to guess it.");
        System.out.println("Please enter in your first guess:");
        int guess1 = sc.nextInt();
        System.out.println("Please enter in your second guess:");
        int guess2 = sc.nextInt();
        System.out.println("Guess#1:" + guess1);
        System.out.println("Guess#2:" + guess2);
        if (guess1 == randomNumber) {
            System.out.println("Good job! Your first guess:" + guess1 + " was exact to my number!");
        } else if (guess2 == randomNumber){
            System.out.println("Good job! Your first guess:" + guess2 + " was exact to my number!");
        } else if (guess1!=randomNumber && (Math.abs(guess2-randomNumber)<Math.abs(guess1-randomNumber))) {
            System.out.println("The number was: " + randomNumber + " ." + " Your second guess was closer.");
        } else if (guess1!=randomNumber && (Math.abs(guess2-randomNumber)<Math.abs(guess1-randomNumber))) {
            System.out.println("The number was: "+randomNumber + " ." + " Your second guess was closer.");
        }
    }
}

and if it is i just would need to add in how to say not to put 2 of the same numbers and to try again. but ill have to get back to this later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.