So I am trying to create a guessing game where the code guesses your number within a given range with a small amount of guesses. The problem I am running into is that my loop does not create a new variable b to recycle through and arrive at the guessed number. Rather it maintains the original value for b. Here is my code:

"’

package guessingame; import java.util.Scanner; /** * * @author User */ public class GuessinGame {

``````public static void main(String[] args) {
System.out.print("I will guess your number! Think of a number. You can only say your number is between some numbers within the range 0 and 100."
+ " My number is between(lower number)...");
//number has to be greater than 0 and less than 100
Scanner ex = new Scanner(System.in);
int x = ex.nextByte();
System.out.print("and....(higher number)");
Scanner why = new Scanner(System.in);
int y = why.nextByte();
int c =  y - x;
double b = c/2; //max b value is 50
while (b < 50 & b > 0){
System.out.print("Is your number " + b + "? (only answer with 'yes' 'lower' or 'higher'");
double j = b/2;
if (ans.equals("yes"))
{System.out.print("Got it!");
break;}
else if (ans.equals("higher"));
{b = (b + j);}
if (ans.equals("lower"));
{b = (b - j);}
}
}
``````

}

'"

also this is my first project in java. Any tips on more efficient organization or better wording etc would be appreciated.

1. In the line,
``````int c = y - x;
double b = c/2;
``````

You are actually subtracting the greater number from smaller number which is wrong because the number which needs to be guessed will always be out of this range.
Example if the number is between 30 and 60, b will be 15, which is clearly not within the specified range. So it should be

``````int c = y + x;
double b = c/2;
``````
1. Do not put semicolon at the end of IF statement. semicolon is used to indicate the end of a statement or block.

Below is the code after making above changes.

``````import java.util.Scanner;
public class GuessinGame {

public static void main(String[] args) {

System.out.print("I will guess your number! Think of a number. You can only say your number is between some numbers within the range 0 and 100."+ " My number is between(lower number)…");

//number has to be greater than 0 and less than 100
Scanner ex = new Scanner(System.in);
int x = ex.nextByte();

System.out.print("and…(higher number)");
Scanner why = new Scanner(System.in);
int y = why.nextByte();

int c = y + x;
double b = c/2; //max b value is 50

while (b < 50 & b > 0)
{
System.out.print("Is your number " + b + "? (only answer with 'yes' 'lower' or 'higher'");

double j = b/2;

if (ans.equals("yes"))
{
System.out.print("Got it!");
break;
}
else if (ans.equals("higher"))
{
b = (b + j);

}
else if (ans.equals("lower"))
{
b = (b - j);

}
}
}
}
``````
1 Like

Just for future reference:

When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text. I was going to edit both of your posts to wrap the code in code blocks, but neither of them seem to have retained any indentation. Just to make it easier to understand, if you could wrap your code in backticks and format it that would be  1 Like

Sorry, I was not aware about it. I have formatted the code now, please check.

Thank you! I feel embarrassed for having missed that.

Thanks, I will remember that.

I changed your category to Help, because you posted Java code which is not the same as JavaScript code.