Hi, i am in need of some help with an assignment.

The Question : Write a program which will think of a word and ask the user to guess the word. The user may guess a letter at a time and your program will print out wherever the letter occurs. If the user guesses incorrectly, they lose a point. If they lose 8 points, they lose the game. If the user guess correctly, then you show where the letter occurs in the word.

My code so far :

public class WordGuess
{
	public static String getRandomWord()
	{
		String [] words = {"keyboard", "mouse", "monitor", "ram", "cpu", "processor", "google", "website", "megabyte", "gigabyte" };
		int randomIndex = (int) (Math.random() * words.length);
		return words[randomIndex];
	}
	
	public static void main (String [] args)
	{
		for(int i = 0; i < words.length; i++)
		{
			System.out.println (words[i]);
		}
	}
}

I know its not much but im kind of stuck so any tips would be nice, thanks :)

Recommended Answers

All 3 Replies

Uhm, read some input and compare it to the randomly selected word?

first of all the compiler must reject this code (compilation error) as the array word is not public , so it is only used inside the scope of the getrandomword method since it was declared in.
so you can declare it outside the method as public static String word[] = bla bla bla
and it will work fine , enjoy

Hey guys, i've pretty much solved the problem i had above.
But now ran into a new one!

Current code :

public class WordGuess
{
	public static String getRandomWord()
	{
		String [] words = {"keyboard", "mouse", "monitor", "ram", "cpu", "processor", "google", "website", "megabyte", "gigabyte" };
		int randomWord = (int) (Math.random() * words.length);
		return words[randomWord];
	}
	
	public static void main (String [] args)
	{
		System.out.println ("Guess a word");
		String Guess1 = Console.readString();
		
		boolean Correct = true; 
		boolean Incorrect = false;
		
		if(Guess1 = Correct)
		{
			System.out.println ("Correct");
		}
		
		else if(Guess1 = Incorrect)
		{
			System.out.println ("Incorrect");
		}
		
		
	}
}

What i want the program to do is, ask the user to enter a letter. If that letter is in the word, it will print "Correct", and if the letter isn't in the word then print "Incorrect"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.