Results 1 to 7 of 7
  1. #1
    Liquid is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default Help debugging and static non static help

    I worked on this for a while but i dont know how to fix the syntax problems. Here is the code, it all needs to be private so it makes it more difficult. Any help would be appritiated.


    Java Code:
    import java.util.Scanner;
    
    public class Hangman{ 
    
      Scanner in= new Scanner(System.in);
      public char[] word ;
     private char[] guessedword;
      private char guess;
     private int wrongGuesses;
      private int blanks;
      
      private void guess()
      {
       boolean letter=false;
       while(letter==false)
       {
       System.out.println("Enter a guess");
       guess=in.nextLine().charAt(1);
       letter= Character.isLetter(guess);
       }
       for(int x=0; x<word.length; x++)
       {
        if(word[x]==guess)
        {
         guessedword[x]=word[x];
         blanks--;
        }
       }
       
      }
     
     private void printman()
     {
      System.out.println("________");
      System.out.println("  |       |");
      if (wrongGuesses>0)
       System.out.println("  |       O");
      else
       System.out.println("  |");
      
      if(wrongGuesses>3)
       System.out.println("  |     / | \\");
      else if (wrongGuesses>2)
       System.out.println("  |     / |");
      else if (wrongGuesses>1)
       System.out.println("  |       |");
      else
       System.out.println("  |");
      
      if (wrongGuesses>1)
       System.out.println("  |       |");
      else
       System.out.println("  |");
      
      if (wrongGuesses>5)
       System.out.println("  |      / \\");
      else if (wrongGuesses>4)
       System.out.println("  |      /");
      else
       System.out.println("  |");
      
      System.out.println("  |______");
      
      for(int x=0; x<word.length; x++)
      {
        if (Character.isLetter(guessedword[x]))
          System.out.print(guessedword[x]);
        else
          System.out.print("_");
      }
      System.out.print("\n");
     }
       
      
      
      public static void main(String[] args) {
       
       System.out.println("please enter a word (all lowecase letters, please)");
       Scanner input= new Scanner (System.in);
       String word1= new String();
       word1=input.nextLine();
       word =word1.toCharArray();
       guessedword= new char[word.length];
       for (int x=0;x<word1.length(); x++)
       {
        blanks++;
       }
       
       while(wrongGuesses<6 && blanks!=0);
       {
        this.guess();
        this.printman();
       }
       
       System.out.println("GAME OVER");
       if(wrongGuesses==6)
        System.out.println("YOU LOSE!");
       else
        System.out.println("YOU WIN!");
      }
    }
    Last edited by Fubarable; 10-25-2010 at 10:04 PM. Reason: making it code

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please tell us what errors you are receiving and which lines are causing them, else it will be hard to help you. Also, you'll want to edit your post above so that the pasted code uses code tags and enables the code to retain its formatting. Please see the first link in my signature to see how to do this.

    Luck!

  3. #3
    Liquid is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default my issue

    the issue is the this.guess and this.printman are not static and cant be called enless they are but if they are i get all sorts of other issues

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're calling the methods in the static main method on "this" and there is no "this" inside of a static method.

    Solution: create a Hangman variable in the main method, initialize it with new Hangman(), and call your methods off of this variable.

    Also (again), please read the link in my signature on using code tags as you're doing them wrong. It's not [java] and [/java], it's [code] and [/cod&#101;]. I'll add them to your code this time, but in the future, please add them yourself.

  5. #5
    Liquid is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default thank you

    Thank you for you help. Sorry i didnt fix the code. I tried that but now all that seems to happen is it asks for the word then stops working. The rest of the code doesnt run

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Liquid View Post
    Thank you for you help. Sorry i didnt fix the code. I tried that but now all that seems to happen is it asks for the word then stops working. The rest of the code doesnt run
    If you want help in understanding why your most recent code isn't working, you'll want to post the code, right?

  7. #7
    Liquid is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    public class Hangman {
    
    Scanner in = new Scanner(System.in);
    public char[] word;
    private char[] guessedword;
    private char guess;
    private int wrongGuesses;
    private int blanks;
    private int secretword;
    
    public Hangman() {
    	System.out.println("please enter a word (all lowecase letters, please)");
    	Scanner input = new Scanner(System.in);
    	String secretword = new String();
    	secretword = input.nextLine();
    	word = secretword.toCharArray();
    	guessedword = new char[word.length];
    	for (int x = 0; x < secretword.length(); x++) {
    	blanks++;
    	}
    
    	while (wrongGuesses < 6 && blanks != 0);
    	{
    	this.guess();
    	this.printman();
    	}
    
    	System.out.println("GAME OVER");
    	if (wrongGuesses == 6)
    	System.out.println("YOU LOSE!");
    	else
    	System.out.println("YOU WIN!");
    	}
    
    
    private void guess() {
    boolean letter = false;
    while (letter == false) {
    System.out.println("Enter a guess");
    guess = in.nextLine().charAt(1);
    letter = Character.isLetter(guess);
    }
    for (int x = 0; x < word.length; x++) {
    if (word[x] == guess) {
    guessedword[x] = word[x];
    blanks--;
    }
    }
    
    }
    
    private void printman() {
    System.out.println("________");
    System.out.println(" | |");
    if (wrongGuesses > 0)
    System.out.println(" | O");
    else
    System.out.println(" |");
    
    if (wrongGuesses > 3)
    System.out.println(" | / | \\");
    else if (wrongGuesses > 2)
    System.out.println(" | / |");
    else if (wrongGuesses > 1)
    System.out.println(" | |");
    else
    System.out.println(" |");
    
    if (wrongGuesses > 1)
    System.out.println(" | |");
    else
    System.out.println(" |");
    
    if (wrongGuesses > 5)
    System.out.println(" | / \\");
    else if (wrongGuesses > 4)
    System.out.println(" | /");
    else
    System.out.println(" |");
    
    System.out.println(" |______");
    
    for (int x = 0; x < word.length; x++) {
    if (Character.isLetter(guessedword[x]))
    System.out.print(guessedword[x]);
    else
    System.out.print("_");
    }
    System.out.print("\n");
    }
    
    
    public static void main(String[] args) {
    Hangman game = new Hangman();
    }
    }
    it asks for the word but after that stops running enless you dont enter anything in which case it asks for a guess. i think because of the way i wrote if char=false it is accepting the initial word as a char and therefore not running

Similar Threads

  1. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  2. Replies: 4
    Last Post: 05-12-2010, 11:05 AM
  3. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •