Results 1 to 8 of 8
  1. #1
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Smile need help with this java code

    hi I've done this java code to make the computer guess a random word from the array

    Java Code:
    import java.util.*;
    
    // bluej is actually a bad name of a class because it should start with capital letter.
    public class WordGuesser {
    
    public void main(String[]args) {
    System.out.println("start");
    ArrayList words = new ArrayList();
    
    
    words.add("hello");
    words.add("car");
    words.add("dog");
    words.add("plane");
    words.add("plant");
    words.add("help");
    words.add("job");
    words.add("java");
    words.add("blue");
    words.add("bathroom");
    
    Die die = new Die(words.size());
    
    System.out.println(die);
    
    
    boolean isCorrect = false;
    while(!isCorrect) {
    System.out.println("What word am I thinking of?");
    String guess = ""; // read in guess using System.in
    
    isCorrect = die.equals(guess);
    if(!isCorrect) {
    System.out.println("Sorry that wasn't correct.");
    } else {
    System.out.println("That's correct. I was thinking of " + die);
    }
    } 
    System.out.println("finish");
    }
    
        }

    I need help to make the computer guess the word letter by letter

    please help cause I'm not that great on java just started learning it a month ago

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    What is Die? What exactly are you having trouble with?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    What is Die? What exactly are you having trouble with?
    Die is a class that makes the computer guess a random word this is it's code

    Java Code:
    public class Die 
    {
      private int noFaces;
      private int faceValue;
    
    
      /**
       * @param faces the number of faces on the new die
       */
      public Die( int faces )
      {
        if (faces > 1)
        {
          noFaces = faces;
        }
        else
        {
          noFaces = 2;
        } // if
    
           // initialise faceValue as a default 
        faceValue = roll(); 
    
      } // constructor
    
    
      /**
       * @return the value the current face is to
       */
      public int getFaceValue()
      // this method may not be used in the completed game
      {
        System.out.println("Face has value: " + faceValue);
        return faceValue;
      } 
    
    
      /**
       * @param newFaceValue  the value to set the face to
       */
      public void setFaceValue(int newFaceValue)
      {
        if (newFaceValue > noFaces) {
          faceValue = noFaces; 
        }
        else {
          faceValue = newFaceValue;
        } 
    
        System.out.println("Face set to value: " + faceValue);
      } 
    
    
      /**
       * @return the value a newly-selected face is set to
       */
      public int roll() 
      {
        faceValue = (int) (Math.random() * noFaces) + 1;
        // System.out.println("New face set to value: " + faceValue);
        return faceValue;
      } 
    
    }
    and what I want help with is the game I'm making



    I'm making a hangman game for the computer

    so a random word from the arraylist is chosen and the computer has to guess it

    with the code that I've got the computer is guessing them word by word

    and I need help making the computer guess them letter by letter and I don't really know how to do it

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Huh?! The computer doesn't guess anything at all. :confused:
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default

    ohhhh I'm mixed up can you help me fix it cause I don't really know what I'm doing

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Same here. I have no idea what you are trying to do. What you have said before isn't reflected anywhere in your code.
    I suggest you start from scratch and do it step by step with lots of debug outputs.

    Nobody here will fix anything for you. You have to do it yourself. We'll be glad to answer specific questions and give advice.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    I think what you want to do is this: instead of

    Java Code:
    isCorrect = die.equals(guess)
    have

    Java Code:
    String wordToTarget = (String)words.get (die.getFaceValue ());
    isCorrect = wordToTarget.equals (word);

  8. #8
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default

    ok thank you both

    I'll start it again and see what I come up with

Similar Threads

  1. java code
    By noor in forum New To Java
    Replies: 2
    Last Post: 01-01-2010, 03:00 PM
  2. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  4. Java Code
    By g.ganiraju in forum New To Java
    Replies: 2
    Last Post: 10-20-2008, 03:18 PM
  5. Need a java code
    By vissu007 in forum New To Java
    Replies: 1
    Last Post: 07-11-2007, 11:06 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
  •