+ Reply to Thread
Results 1 to 6 of 6

Thread: Guess The Word.

  1. #1
    Newbie Paradox has a spectacular aura about Paradox has a spectacular aura about Paradox has a spectacular aura about
    Join Date
    Jul 2008
    Posts
    17

    Guess The Word.

    I was just messing around today because i was board so i decided to make a program in Java where you can either make your own word for somebody to guess or you can have it select a random one for you.

    Code:
    import java.util.*;
    
    public class wordGuess {
    
        public static String word = null;
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("This is a guessing words game.");
            System.out.println("Type the word to use or type random to use a random word.");
            word = input.nextLine();
            if(word.equals("random")) {
                switch (rand(11)) {
                case 0:
                    word = "hello";
                    break;
                case 1:
                    word = "bye";
                    break;
                case 2:
                    word = "what";
                    break;
                case 3:
                    word = "cool";
                    break;
                case 4:
                    word = "hi";
                case 5:
                    word = "sweet";
                    break;
                case 6:
                    word = "call of duty";
                    break;
                case 7:
                    word = "funny";
                    break;
                case 8:
                    word = "woof";
                    break;
                case 9:
                    word = "computer";
                    break;
                case 10:
                    word = "laptop";
                    break;
                    
                }
            }
            wordOne();
        }
        private static int rand(int bound) {
            return (int) (Math.random() * bound);
        }
        public static void wordOne() {
            int tries = 0;
            boolean gotIt = false;
            String inputWord = null;
            Scanner input = new Scanner(System.in);
            System.out.println("You will have 50 tries to get this word");
            if(tries >= 49) {
                System.out.println("You have exided the maximum number of tries.");
                gameOver();
            }
            while (tries <= 50) {
                inputWord = null;
                System.out.println("Enter a letter you think the word may contain");
                System.out.println("If you want to try and guess just type the word.");
                System.out.println("");
                inputWord = input.nextLine();
                tries++;
                
                    if(inputWord.equals(word)) {
                        System.out.println("Youve got it nice one!!");
                        sleep(1000);
                        return;
                    }
                if(word.contains(inputWord)) {
                    System.out.println("You have guessed right, it does contain a "+inputWord);
                }
                else
                {
                    System.out.println("Sorry you have guessed wrong.");
                }
            }
            
        }
    
        public static void gameOver() {
            System.out.println("Thanks for playing my little game!");
        } 
        public static void sleep(int time) {
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        
        }
    }

  2. #2
    Retired v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light v0id is a glorious beacon of light
    Join Date
    Apr 2007
    Posts
    2,978
    Blog Entries
    3

    Re: Guess The Word.

    I only checked your code briefly, mainly because I'm not a Java programmer.
    One obvious thing I would make different is how you choose the random word. Instead of using a switch-statement I would keep all of the random words in an array. Then I would take the size of the array and generate a random number between 0 and the size of the array. This number will then be used for accessing the array.
    Instead of the many, many lines you have, you could solve it in two lines of code.

  3. #3
    Newbie Paradox has a spectacular aura about Paradox has a spectacular aura about Paradox has a spectacular aura about
    Join Date
    Jul 2008
    Posts
    17

    Re: Guess The Word.

    Thanks for the help.

    I was bored so I believe there would be a lot of mistakes and didn't rely spend time on it.

    ~Paradox

  4. #4
    Newbie shoaibbi is an unknown quantity at this point shoaibbi's Avatar
    Join Date
    Jan 2009
    Posts
    15

    Re: Guess The Word.

    gud one
    VIvAcIoUs pAkIsTaNi

  5. #5
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    Re: Guess The Word.

    Why did you override the sleep method? Also your wordOne method seems a bit long. Could it be broken down a bit more?

    Another thing, why do you make a random method, that seems a bit pointless to replace 1 line of code. Your program only runs once, so do you need the rand method?
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  6. #6
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    Re: Guess The Word.

    Quote Originally Posted by chili5 View Post
    Why did you override the sleep method? Also your wordOne method seems a bit long. Could it be broken down a bit more?

    Another thing, why do you make a random method, that seems a bit pointless to replace 1 line of code. Your program only runs once, so do you need the rand method?
    He could do that or make a simple AI

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Word 2003 Resizes Pages by Itself
    By dargueta in forum Computer Software/OS
    Replies: 1
    Last Post: 07-12-2008, 05:00 AM
  2. Guess a Number
    By 3n! in forum C and C++
    Replies: 2
    Last Post: 12-02-2007, 03:44 PM
  3. Dictonary Program
    By programmer 101 in forum Java Help
    Replies: 9
    Last Post: 07-01-2007, 01:39 PM
  4. Linking one word to another on same page
    By PC101 in forum HTML Programming
    Replies: 4
    Last Post: 09-21-2006, 12:38 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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