Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 8th, 2012, 08:03 AM
ashboi ashboi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 ashboi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 58 sec
Reputation Power: 0
Unhappy Simple Problem in my code... Help

This is sample of my code. I need be able to have a cumulative score after the user plays x number of times. But keep getting same answer.

import java.util.Scanner;
public class Hangman
{

public static void main(String[] args)
{
System.out.println("Welcome to Hangman... v1.0 - Designed Ashley Bwanya");
initGame();
}

public static void initGame() {


int errors = 0;
int cumScore = 0,score;
String word;
String displayHidden = "";
String used = "";

Game myWord = new Game();
char guess;


word = myWord.getWord();



for (int i = 0; i < word.length(); i++)
{
displayHidden += "*";
}

StringBuilder guessedWord = new StringBuilder(word);
StringBuilder test = new StringBuilder(displayHidden);



System.out.println("This is the secret word you have to guess: \n");
System.out.println(test+ "\n");


while(guessedWord.toString().equals(test.toString( )) == false && errors < 5)
{
char[] lettersGuessed = new char[10] ;
System.out.print("Guess Letter: ");
Scanner scan = new Scanner(System.in);
guess = scan.next().charAt(0);

//System.out.println("Letters Already Used: "+ lettersGuessed[0]);
for(int i = 0; i < guessedWord.length(); i++){ //check if letter has been guessed
if(guessedWord.charAt(i) == guess)
{
test.setCharAt(i, guess);
System.out.println(test);
}

}
if(word.indexOf(guess) == -1 ) //if letter not in secret then errors goes up by 1 and while loop continues
{
errors++;
}

if(guessedWord.toString().equals(test.toString( )) == true)
{
score = (5 - errors);
cumScore += score;
System.out.println("You win !!!");
System.out.println("Your cumlative score is: " + cumScore);
initGame();
}

if(errors == 5)
{
score = (5 - errors);
String name;
Scanner input = new Scanner(System.in);
System.out.println("You lose !!!");
System.out.println("Your score is: " + score);
System.out.print("Please enter your name: ");
name = input.nextLine();
myWord.setSaveScore(name, score);

}


}



}

}

Reply With Quote
  #2  
Old November 8th, 2012, 10:29 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
How many places have you posted this question?

Did you look at and respond to the responses made on the other sites?

Also at http://www.javaprogrammingforums.com/java-theory-questions/19100-basic-java-calculating-cumulative-score-hangman-help.html
and
http://www.javaprogrammingforums.co...e-sum-code.html

Reply With Quote
  #3  
Old January 30th, 2013, 02:40 AM
rhae29 rhae29 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 rhae29 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 51 sec
Reputation Power: 0
try to follow his suggestion. i'll also try to search other site that can help and solve your problem..

Reply With Quote
  #4  
Old January 30th, 2013, 08:02 PM
CraigR15 CraigR15 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 CraigR15 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 32 sec
Reputation Power: 0
Try looking into static fields. You can declare a static field for an Object which allows one value for all objects you initialize. You can then simply do a count each time (E.g) the Game object is called.

Also looking at your code I notice this line:

if(guessedWord.toString().equals(test.toString( )) == true)

You are using unnecessary code. The if statement automatically continues if the boolean expression is true, you don't need to specify this.

You should use:
if(guessedWord.toString().equals(test.toString( )))

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Simple Problem in my code... Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap