High Low game

I'm really new to java, and I've only learned basic things.

I need to make a program where you think of the number in your head, between 0 - 100, and the computer guesses, starting at 50. If it's fifty, you tell it that it's correct, if it's higher, you tell it higher, and if it's lower, tell it it's lower.

I don't have the exact source, but I'll rewrite it kinda here.

I have it guessing, and have it guess higher than the guess or lower.

// input stuff is up here

int a, b, c, d;

a = 50;// Computer Guess

b = 0;// Input High Low Correct

c = 0;// Lowest

d = 100;// Highest

e = 0;// Finished Loop

System.out.println("Think of a number, I'll guess it.");

while(e != 1){

System.out.println("Is your number " + a +"?");

System.out.println("\"1\" Higher, \"2\" Lower, \"3\" Correct");

b = HiLow4.readInt();// Reads number for switch statement!

switch(b){

case 1://Higher

c = a;// Sets lowest number to the current guess

a = (a + (d / 4));// This is wrong, I need help with it

break;

case 2://Lower

d = a;//Highest number to current guess

a = (a - (d / 4);//Wrong, need help

case 3://Correct

System.out.println("I knew I could get it!");

e = 1;

default:

System.out.println("Try using the right numbers");

}//Switch

}//While

That doesn't work right, and I can't figure it out.

I know most basic java things, and we're using J2SDK1.4.0_03 :(

Our school hates us nerds.

Can anyone help me?

[2645 byte] By [the_V_Dudea] at [2007-11-26 18:03:40]
# 1
what does "not work right" look like?this isn't compilable. not legal java. post the whole mess if you want help.%
duffymoa at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 2
Use more descriptive variable names, that way you won't need useless comments. Repost the code once you have fixed the variable names.
CaptainMorgan08a at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 3

I don't have the whole program, it's at school.

by not working right, I mean that it will run it, but it won't guess all the numbers.

It'll guess over 100, and less than 0. The problem is in the math.

If you can't help me it's fine, I might be able to figure it out.

// input stuff is up here

int guess, inputHLC, low, high, done;

guess = 50;

inputHLC = 0;

low = 0;

high = 100;

done = 1;

System.out.println("Think of a number, I'll guess it.");

while(done != 0){

System.out.println("Is your number " + guess + "?");

System.out.println("\"1\" Higher, \"2\" Lower, \"3\" Correct");

inputHLC = HiLow4.readInt(); // Reads number for switch statement!

switch(b){

case 1: //Higher

low = guess;

guess = (guess + (high / 4)); // Math is wrong

break;

case 2: //Lower

high = guess;

guess = (guess - (high / 4); //Math is wrong

break;

case 3: //Correct

System.out.println("I knew I could get it!");

done = 0;

break;

default:

System.out.println("Try using the right numbers");

}//Switch

}//While

The math is wrong, I'll figure it out though

Message was edited by:

the_V_Dude

the_V_Dudea at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 4

thefifthdoodette,

Collaborative problem solving is impossible with a partner who sits there with the attitude "Just gimme the answers".

read [url=http://forum.java.sun.com/help.jspa?sec=formatting]this[/url] and even [url=http://www.catb.org/~esr/faqs/smart-questions.html]this[/url]

but at least take the above advise ... in order to assist we need a high level problem description, the complete [code]source source[/code], any [pre]compiler errors/warnings &/or runtime errors[/pre]... we need to know the inputs, and expected outputs.... we need to know what you're tried, and why (you think) it didn't work.

keith.

corlettka at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 5
if you don't have access to the code and a compiler right now, then it's pretty pointless posting here.
corlettka at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 6
Mine works nicely and is pretty clean - just 75 lines of code, including whitespace and comments. I'm one of those folks who likes to put braces on the next line, so it's not too hard to write.%
duffymoa at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 7

that was fun!package forums;

import java.util.Scanner;

import java.util.Set;

import java.util.HashSet;

class Guesserator

{

public static void main(String[] args)

{

try {

Scanner scanner = new Scanner(System.in);

Set<Integer> guesses = new HashSet<Integer>(12);

int guess = 50;

int lowest = 0;

int highest = 100;

boolean done = false;

System.out.println("Think of a number, and I'll guess it.");

while(!done) {

int previousGuess = guess;

if (!guesses.add(guess)) {

System.out.println("You're pullin my chain right! It's gotta be "+previousGuess+", you're just a sore loser! I'll teach you!");

for(int i=1; i<=100; i++) {

System.out.print("\rFormatting C: "+i+"%");

try { Thread.sleep(50); } catch (InterruptedException ignore) {}

}

System.out.println("\rJust kidding :-)");

System.exit(1);

}

System.out.print("Is your number (Lower, Equal, Higher, Quit) than " + guess + " : ");

char choice = scanner.nextLine().toUpperCase().charAt(0);

switch(choice){

case 'L': // LOWER: guess > number, so reduce the quess

highest = guess;

guess = lowest + ((highest - lowest) / 2);

break;

case 'H': // HIGHER: guess < number, so raise the quess

lowest = guess;

guess = lowest + ((highest - lowest) / 2);

break;

case 'E': // EQUALS: quess == number, so exit

System.out.println(guess+", I knew it all along!");

done = true;

break;

case 'Q': // QUIT: the user wants out!, so exit.

System.out.println("Aunty Internal Revenue System, The users won't play with me anymore !!!");

done = true;

break;

default:

System.out.println("please type L, E, H, or Q an press return.");

break;

}

//System.err.println("DEBUG: highest="+highest+", lowest="+lowest+", guess="+guess);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

Yeah, Yeah, I'll go get a life... I hear K-Mart's got 'em on special for $13.95.

keith.

corlettka at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 8

@corlettk

Thanks for the program.

My math was wrong, which is what I already knew. Thanks, I was getting closer to it. I knew I needed both high and low in the equation, I couldn't think it through all the way, though.

I feel really stupid, because I'm excelling and acc. trig and I couldn't figure out the math of it. Thanks, now I realize how stupid it was.

Super easy program, I feel stupid.

Troll much? It's the_V_Dude. The V is a letter, and Dude doesn't have an "O" in it, and I'm a guy.

I searched for it in google and then I posted, so help yourself and don't try and argue. From LOOKING ON THE FORUM, there's something about flaming already going on.

the_V_Dudea at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 9
theFifitDoodette, yeah, I'll help... at the expense of a couple of cheep shots. my nickname is "chewie", for which I am busy taking revenge not just upon the Imperial Army, but the universe in general. :-) keith.
corlettka at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 10

I did something similar a while back. The computer has the number and you guess it. Dunno if you need it but :).

NOTE:: some minor errors but too lazy to fix them.

import java.util.Scanner;

import java.util.Random;

public class HiLo

{

static Scanner sc = new Scanner(System.in);

public static void main (String[] args)

{

final int MAX = 100;

int answer, guess, count = 0;

Random generator = new Random();

answer = generator.nextInt(MAX) + 1;

System.out.print ("I'm thinking of a number between 1 and "

+ MAX + ". Guess what it is: ");

guess = sc.nextInt();

while (guess != 0)

{

if (guess > 100 || guess < 1)

{

System.out.println ("I said between 1 and 100. Reenter your guess: ");

guess = sc.nextInt();

}

count ++;

if (guess == answer)

{

System.out.println ("You got it! Good guessing!");

guess = 0;

}

else

{

if (guess > answer)

{

System.out.println ("Your guess is too high!. Guess again: ");

guess = sc.nextInt();

}

if (guess < answer)

{

System.out.println ("Your guess is too low!. Guess again: ");

guess = sc.nextInt();

}

}

}

System.out.println ("Number of guesses: " + count);

}

}

demoNMaCHIN3a at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 11

Naturally, I think mine's better:

[code]

package games;

import java.util.Random;

import java.util.Scanner;

/**

* Guessing game

* Date: Feb 10, 2007

* Time: 8:53:05 PM

*/

public class GuessingGame

{

public static final int MAX_VALUE = 100;

private int guessCount;

private int value;

private static final String LOWER = "Your guess is lower";

private static final String HIGHER = "Your guess is higher";

private static final String CORRECT = "You are CORRECT, sir!";

public static void main(String[] args)

{

GuessingGame game = new GuessingGame();

Scanner console = new Scanner(System.in);

System.out.println("Guess a number between zero and " + MAX_VALUE);

int value = 0;

String result = "";

int i = 0;

do

{

System.out.print("guess # " + ++i + ": ");

value = console.nextInt();

result = game.guess(value);

System.out.println(result);

} while (!CORRECT.equals(result));

System.out.println("You guessed the correct answer in " + game.getGuessCount() + " tries");

}

public GuessingGame()

{

Random random = new Random();

this.value = random.nextInt(MAX_VALUE+1);

this.guessCount = 0;

}

public int getGuessCount()

{

return guessCount;

}

public int getValue()

{

return value;

}

public String guess(int x)

{

String result = "";

++guessCount;

if (x < value)

result = LOWER;

else if (x > value)

result = HIGHER;

else

result = CORRECT;

return result;

}

}

/code]

%

duffymoa at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 12

However, my use of code tags and the preview button suck:

package games;

import java.util.Random;

import java.util.Scanner;

/**

* Guessing game

* Date: Feb 10, 2007

* Time: 8:53:05 PM

*/

public class GuessingGame

{

public static final int MAX_VALUE = 100;

private int guessCount;

private int value;

private static final String LOWER = "Your guess is lower";

private static final String HIGHER = "Your guess is higher";

private static final String CORRECT = "You are CORRECT, sir!";

public static void main(String[] args)

{

GuessingGame game = new GuessingGame();

Scanner console = new Scanner(System.in);

System.out.println("Guess a number between zero and " + MAX_VALUE);

int value = 0;

String result = "";

int i = 0;

do

{

System.out.print("guess # " + ++i + ": ");

value = console.nextInt();

result = game.guess(value);

System.out.println(result);

} while (!CORRECT.equals(result));

System.out.println("You guessed the correct answer in " + game.getGuessCount() + " tries");

}

public GuessingGame()

{

Random random = new Random();

this.value = random.nextInt(MAX_VALUE+1);

this.guessCount = 0;

}

public int getGuessCount()

{

return guessCount;

}

public int getValue()

{

return value;

}

public String guess(int x)

{

String result = "";

++guessCount;

if (x < value)

result = LOWER;

else if (x > value)

result = HIGHER;

else

result = CORRECT;

return result;

}

}

%

duffymoa at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...
# 13
> However, my use of code tags and the preview button> suck:...and use of the edit button. ;-)
CaptainMorgan08a at 2007-7-9 5:33:53 > top of Java-index,Java Essentials,Java Programming...