Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Java: Cumulative Sum and While Loop

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-04-2010, 09: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 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(); } } } } }
 11-05-2010, 02:36 AM PM User | #2 Gox Regular Coder     Join Date: May 2006 Location: Ontario, Canada Posts: 392 Thanks: 2 Thanked 20 Times in 20 Posts 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.
 11-07-2010, 12:17 AM PM User | #3 striker4you New to the CF scene   Join Date: Nov 2010 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts i tried that but i cant seem to get the variables defined when i run?
 11-11-2010, 06:30 AM PM User | #4 Gox Regular Coder     Join Date: May 2006 Location: Ontario, Canada Posts: 392 Thanks: 2 Thanked 20 Times in 20 Posts Can you further explain, or show what you mean by "cant seem to get the variables defined when i run"?

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 03:18 PM.