Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-04-2010, 08:16 AM   PM User | #1
striker4you
New to the CF scene

 
Join Date: Nov 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
striker4you is an unknown quantity at this point
Java: Cumulative Sum and While Loop

For my assignment i am supposed to create a guessing game and I did, and it works but then also count the number of guesses the user has tried and that works BUT when I tried to do a total sum of the # of guesses of all the game the user plays, it doesn't work?

import java.util.*;
public class GuessingGame {
public static final String Y = "Y";
public static final String y = "y";
public static void main(String[] args) {
//main game
game();
}
//while loop for guessing
public static void game() {
Random rand = new Random();
Scanner console = new Scanner(System.in);

int randomNumber = rand.nextInt(1);
int generate = 0;
int tries = 1;
int totalGames = 0;

while (generate == 0){
System.out.println("I'm thinking of a number between 1 and 100...");
System.out.print("Your guess? ");
int guess = console.nextInt();

if(guess < randomNumber){
System.out.println("It's higher. ");
tries++;
}

if(guess > randomNumber){
System.out.println("It's lower. ");
tries++;
}
if(guess == randomNumber){
if ( tries == 1){
System.out.println("You got it right in 1 guess!");
}else if (tries > 1) {
System.out.println("You got it right in " +tries +" guesses!");
generate = 0;
}
System.out.print("Do you want to play again? ");
String word = console.next();

if (word.startsWith(Y)) {
game();
}
if (word.startsWith(y)) {
game();

}else if (!word.equals(Y)) {
totalGames++;
System.out.println("Total games = " +totalGames);
System.out.println("Total guesses =" +totalGames*tries);
word = console.next();
}
}
}
}
}
striker4you is offline   Reply With Quote
Old 11-05-2010, 01:36 AM   PM User | #2
Gox
Regular Coder

 
Gox's Avatar
 
Join Date: May 2006
Location: Ontario, Canada
Posts: 392
Thanks: 2
Thanked 20 Times in 20 Posts
Gox will become famous soon enough
Each time your player starts a new game the following method is called: game()

The first thing the method game() does is reset your variables:
int tries = 1;
int totalGames = 0;

This overwrites the totals from any previous games. Maybe you want to consider moving these variables outside of the method game() so they don't get overwritten.
Gox is offline   Reply With Quote
Old 11-06-2010, 11:17 PM   PM User | #3
striker4you
New to the CF scene

 
Join Date: Nov 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
striker4you is an unknown quantity at this point
i tried that but i cant seem to get the variables defined when i run?
striker4you is offline   Reply With Quote
Old 11-11-2010, 05:30 AM   PM User | #4
Gox
Regular Coder

 
Gox's Avatar
 
Join Date: May 2006
Location: Ontario, Canada
Posts: 392
Thanks: 2
Thanked 20 Times in 20 Posts
Gox will become famous soon enough
Can you further explain, or show what you mean by "cant seem to get the variables defined when i run"?
Gox is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:01 PM.


Advertisement
Log in to turn off these ads.