6 Replies - 921 Views - Last Post: 09 October 2012 - 08:52 PM Rate Topic: -----

#1 tjarmon52  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 4
  • Joined: 09-October 12

guessing game

Posted 09 October 2012 - 08:07 PM

I have to make a guessing game using java this is what i have so far. Im having a hard time trying to figure out how to keep track of the numbers so i can compare them. For example if the random number is 62 and the user enters 34 it needs to say too low but if the user then enters 32 after 34 it needs to say something like if 34 is too low 34 is also too low. And im also need help when i print out do you want to play again (y/n) i dont know how to make it play the game again. Please help!!!!



public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard= new Scanner(System.in);
int secretNumber=(int) (Math.random()* 99 + 1);
int numberOfTries=0;
int max=0;
int min=0;
boolean win= false;
String yes= "y";
String no= "n";

int round=0;
while(round>=0){
round++;

while (win == false){
System.out.println("Guess a number: ");
int guess=keyboard.nextInt();
numberOfTries++;



if (guess>100){
System.out.println("Huh? Your guess should be between 1 and 100");
}
else if (guess==secretNumber){
win= true;
}
else if (guess < secretNumber){
if(guess<=0){
System.out.println("Huh? Your guess should be between 1 and 100");
}else
System.out.println(guess + " is too low");
}
else if (guess > secretNumber){
System.out.println(guess + " is too high");
}
}
System.out.println("That's right! you guessed it in " + numberOfTries + " attempt(s)");
max=Math.max(max, numberOfTries);
min=Math.min(min, numberOfTries);
int avg=numberOfTries/round;
System.out.println("Would you like to play anoter game? (y/n): ");
String x=keyboard.next();

if (x.equalsIgnoreCase(yes)){
//make the program repeat
}


if(x.equalsIgnoreCase(no)){
System.out.println("Your best score was " + max + " tries");
System.out.println("Your worst score was " + min + " tries");
System.out.println("Your average score was " + avg);
}

}
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: guessing game

#2 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

Re: guessing game

Posted 09 October 2012 - 08:14 PM

First step: put your code in code tags.

This post has been edited by Zoquo: 09 October 2012 - 08:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 tjarmon52  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 4
  • Joined: 09-October 12

Re: guessing game

Posted 09 October 2012 - 08:28 PM

View PostZoquo, on 09 October 2012 - 08:14 PM, said:

First step: put your code in code tags.




I have to make a guessing game using java this is what i have so far. Im having a hard time trying to figure out how to keep track of the numbers so i can compare them. For example if the random number is 62 and the user enters 34 it needs to say too low but if the user then enters 32 after 34 it needs to say something like if 34 is too low 34 is also too low. And im also need help when i print out do you want to play again (y/n) i dont know how to make it play the game again. Please help!!!!



public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard= new Scanner(System.in);
int secretNumber=(int) (Math.random()* 99 + 1);
int numberOfTries=0;
int max=0;
int min=0;
boolean win= false;
String yes= "y";
String no= "n";

[code]
int round=0;
while(round>=0){
round++;
[code/]

[code]
while (win == false){
System.out.println("Guess a number: ");
int guess=keyboard.nextInt();
numberOfTries++;
[code/]

[code]
if (guess>100){
System.out.println("Huh? Your guess should be between 1 and 100");
}
else if (guess==secretNumber){
win= true;
}
else if (guess < secretNumber){
if(guess<=0){
System.out.println("Huh? Your guess should be between 1 and 100");
}else
System.out.println(guess + " is too low");
}
else if (guess > secretNumber){
System.out.println(guess + " is too high");
}
}
[code/]

[code]
System.out.println("That's right! you guessed it in " + numberOfTries + " attempt(s)");
max=Math.max(max, numberOfTries);
min=Math.min(min, numberOfTries);
int avg=numberOfTries/round;
System.out.println("Would you like to play anoter game? (y/n): ");
String x=keyboard.next();
[code/]


[code]
if (x.equalsIgnoreCase(yes)){
//make the program repeat
}


if(x.equalsIgnoreCase(no)){
System.out.println("Your best score was " + max + " tries");
System.out.println("Your worst score was " + min + " tries");
System.out.println("Your average score was " + avg);
}[code/]

}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard= new Scanner(System.in);
int secretNumber=(int) (Math.random()* 99 + 1);
int numberOfTries=0;
int max=0;
int min=0;
boolean win= false;
String yes= "y";
String no= "n";

int round=0;
while(round>=0){
round++;

while (win == false){
System.out.println("Guess a number: ");
int guess=keyboard.nextInt();
numberOfTries++;



if (guess>100){
System.out.println("Huh? Your guess should be between 1 and 100");
}
else if (guess==secretNumber){
win= true;
}
else if (guess < secretNumber){
if(guess<=0){
System.out.println("Huh? Your guess should be between 1 and 100");
}else
System.out.println(guess + " is too low");
}
else if (guess > secretNumber){
System.out.println(guess + " is too high");
}
}
System.out.println("That's right! you guessed it in " + numberOfTries + " attempt(s)");
max=Math.max(max, numberOfTries);
min=Math.min(min, numberOfTries);
int avg=numberOfTries/round;
System.out.println("Would you like to play anoter game? (y/n): ");
String x=keyboard.next();

[code]
if (x.equalsIgnoreCase(yes)){
//make the program repeat
}
[code/]

if(x.equalsIgnoreCase(no)){
System.out.println("Your best score was " + max + " tries");
System.out.println("Your worst score was " + min + " tries");
System.out.println("Your average score was " + avg);
}

}
}
}
Was This Post Helpful? -1
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: guessing game

Posted 09 October 2012 - 08:34 PM

Sigh...Step by step guide here we go.
Try your post again.
Tell us EXACTLY where you need help.
Copy/paste code. Highlight code. Press the code image icon above this text area.
Then use preview to make sure everything looks right.
Press submit.
Was This Post Helpful? 1
  • +
  • -

#5 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

Re: guessing game

Posted 09 October 2012 - 08:34 PM

Second step: put your code in code tags.
Was This Post Helpful? 0
  • +
  • -

#6 tjarmon52  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 4
  • Joined: 09-October 12

Re: guessing game

Posted 09 October 2012 - 08:47 PM

I have to make a guessing game using java this is what i have so far. Im having a hard time trying to figure out how to keep track of the numbers so i can compare them. For example if the random number is 62 and the user enters 34 it needs to say too low but if the user then enters 32 after 34 it needs to say something like if 34 is too low 34 is also too low. And im also need help when i print out do you want to play again (y/n) i dont know how to make it play the game again. Please help!!!!



public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard= new Scanner(System.in);
int secretNumber=(int) (Math.random()* 99 + 1);
int numberOfTries=0;
int max=0;
int min=0;
boolean win= false;
String yes= "y";
String no= "n";

int round=0;
while(round>=0){
round++;

while (win == false){
System.out.println("Guess a number: ");
int guess=keyboard.nextInt();
numberOfTries++;



if (guess>100){
System.out.println("Huh? Your guess should be between 1 and 100");
}
else if (guess==secretNumber){
win= true;
}
else if (guess < secretNumber){
if(guess<=0){
System.out.println("Huh? Your guess should be between 1 and 100");
}else
System.out.println(guess + " is too low");
}
else if (guess > secretNumber){
System.out.println(guess + " is too high");
}
}
System.out.println("That's right! you guessed it in " + numberOfTries + " attempt(s)");
max=Math.max(max, numberOfTries);
min=Math.min(min, numberOfTries);
int avg=numberOfTries/round;
System.out.println("Would you like to play anoter game? (y/n): ");
String x=keyboard.next();

[code]
Here i need to make the guessing game repeat if the user enters a y
if (x.equalsIgnoreCase(yes)){
//make the program repeat
}

[code/]

if(x.equalsIgnoreCase(no)){
System.out.println("Your best score was " + max + " tries");
System.out.println("Your worst score was " + min + " tries");
System.out.println("Your average score was " + avg);
}

}
}
}
Was This Post Helpful? -1
  • +
  • -

#7 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: guessing game

Posted 09 October 2012 - 08:52 PM

Go to full editor. Use preview before you post anything. If your code is not in code tags. Do not bother posting. If you decide to keep trolling, we can all just wait for the admins. If you actually want help I suggest you put a bit of a effort besides copying and pasting the same post over and over..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1