Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Please help with cumulative sum code...

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Please help with cumulative sum code...

    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);
     
    			 }
     
     
    		  }
     
     
     
    		}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please help with cumulative sum code...

    Is this the same problem:
    http://www.javaprogrammingforums.com...gman-help.html

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    On the other post I said:
    Define a variable that is saved over the life of the execution of the code and add the scores to it.
    The posted code does not do that. The variable needs to be defined outside of any methods if you want to preserve its contents.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please help with cumulative sum code...

    Tried it but the method cannot reach the varible. And yes its the same problem

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please help with cumulative sum code...

    Tried it but the method cannot reach the varible
    Please post the code with the error and post the full text of the compiler's error message.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please help with cumulative sum code...

    import java.util.Scanner; 
    public class Hangman
    {	
    	int cum;
    	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)
    		  {  
    		  	 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);
    				cum += 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);
     
    			 }
     
     
    		  }
     
     
     
    		}
     
    }

    Hangman.java:64: error: non-static variable cum cannot be referenced from a static context
    cum += score;
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please help with cumulative sum code...

    non-static variable cum cannot be referenced from a static context
    cum needs to be defined as static if you want to use it in a static method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    ashboi (November 8th, 2012)

  8. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool Re: Please help with cumulative sum code...

    Quote Originally Posted by Norm View Post
    cum needs to be defined as static if you want to use it in a static method.
    Thanks a million... Works perfectly. Defined it as
     static int cum

Similar Threads

  1. Basic Java Calculating Cumulative Score - Hangman Help
    By ashboi in forum Java Theory & Questions
    Replies: 3
    Last Post: November 8th, 2012, 08:49 AM
  2. code not returning sum
    By hmcka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2012, 09:17 AM
  3. Sum of intervals
    By cisneros778 in forum Java Theory & Questions
    Replies: 3
    Last Post: February 21st, 2012, 04:08 PM
  4. Cumulative sum of entries
    By CSUTD in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 1st, 2011, 06:32 PM
  5. [SOLVED] Cumulative array
    By spaggwoo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 23rd, 2011, 09:54 AM