0

so I'm working on making a simple hangman game on Java. The computer should choose a random word from a set of words :

public void setWords() {
    words[0] = "notions";
    words[1] = "measure";
    words[2] = "product";
    words[3] = "foliage";
    words[4] = "garbage";
    words[5] = "minutes";
    words[6] = "chowder";
    words[7] = "recital";
    words[8] = "concoct";
    words[9] = "brownie";       
}

I'm trying to write the code to generate a random word when the player plays: I have this as a starter:

public class Hangman {
private int numwords = 10;
private String[] words = new String[numwords];
private String gameWord;
private String dispWord = "-------";
private char[] dispArr = dispWord.toCharArray();



private static void main(String[] args) {
    System.out.println("Welcome to Hangman!:");

    Random rand= new Random();
    char c = rand.nextChar(setWords);
}

Could you please help with the syntax of choosing a random word using the selectGameWord() method? Thank you!

Emily
  • 21
  • 7

3 Answers3

0

In your main you could have the game word selected like this.

String gameWord = words[rand.nextInt(words.length)];

The bound of your random number will be from 0 <= x <= 9 if you do `.nextInt(10)'. Because your word array has only ten set choices.

Yash Mehra
  • 169
  • 1
  • 6
-1

I believe you want to get a random word from your array? You have to use Random#nextInt() to get a random index from your array.

Random random = new Random();
int index = random.nextInt(words.length);
String randomWord = words[index];
-2

https://stackoverflow.com/a/5887745/6934695

You can use this one for get random number.

    import java.util.Random;

Random rand = new Random();    
int  n = rand.nextInt(words.length);
String word=selectGameWord(n)

and selectGameWord(int x);

 sentence=words[x];
   return sentence;

Edit:

import java.util.Random;

public class Hangman{
    String words[]={"notions","measure","product","foliage"};



    public String selectGameWord(int x)
    {
        String sentence= words[x]; 
        return sentence; 
    }
    public static void main (String[] args){
        System.out.println("Welcome to Hangman!:"); 
        Random rand=new Random(); 
        Hangman myhangman= new Hangman();
        int n= rand.nextInt(myhangman.words.length);        
        String word= myhangman.selectGameWord(n); 
        System.out.println(word);
    }
}
Community
  • 1
  • 1
nerdicsapo
  • 427
  • 5
  • 16
  • but then I should change the static part to just public void main(String[] args){ right? – Emily Mar 12 '17 at 18:59
  • If you have just one class, you don't have to. – nerdicsapo Mar 12 '17 at 19:14
  • because when I had this code it gave me several errors saying that I "cannot make a static reference to non-static field-words" – Emily Mar 12 '17 at 19:22
  • I have a public class hangman{ and public static void main(String[] args){ inside the first class and this is where I had the random code written – Emily Mar 12 '17 at 19:22
  • Create a object. Then call the selectGameWord method. – nerdicsapo Mar 12 '17 at 20:17
  • Let's say your class name is Sapo. Sapo sapo = new Sapo(); String word = sapo.selectGameWord(n); – nerdicsapo Mar 12 '17 at 20:18
  • it's still gonna be under the main right? public static void main(String[] args){ System.out.println("Welcome to Hangman!:"); //pick a random word from the array Random rand=new Random(); int n= rand.nextInt(words.length); String word=selectGameWord(n); selectGameWord(int x); sentence= words[x]; return sentence; Hangman myhangman= new Hangman(); String word= myhangman.selectGameWord(n); – Emily Mar 12 '17 at 20:43
  • I edited my answer, check it out. – nerdicsapo Mar 12 '17 at 21:05
  • I got that part down! Thank you! – Emily Mar 13 '17 at 00:31
  • Done! The tick is now green! :) – Emily Mar 13 '17 at 01:59