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(); } } }
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.
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
gud one
VIvAcIoUs pAkIsTaNi
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?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks