我如何创建一个结合两个变量的变量?

时间:2014-09-07 23:28:25

标签: java string variables random int

我尝试在线查看并没有罚款任何有用的东西,我正在制作一个刽子手游戏,我想要一个字符串,从一大堆的单词中得到一个随机的单词,我想如果我把我的所有单词变量像word1,并创建一个等于word加上随机整数Ex的String。 String randomWord = word+randomInt;然后创建了一个随机数生成器,它将生成一个随机数并将其设置为randomInt,但是当我这样做时,它告诉我该单词不是变量,我试着将这些东西放在整个语句中,但是它不起作用,我怎么能这样做?

CODE:

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;

public class hangMan {


    public static void main(String[] arg) {

        Random random = new Random();

        int randomInt = random.nextInt(2) + 1;

        System.out.println("Random Integer in Java: " + randomInt);


        //word letters
        final String word1Letter1 = "l";
        final String word1Letter2 = "u";
        final String word1Letter3 = "d";
        final String word1Letter4 = "e";
        final String word1 = "lude";

        final String word2Letter1 = "l";
        final String word2Letter2 = "o";
        final String word2Letter3 = "s";
        final String word2Letter4 = "e";
        final String word2 = "lose";

        final String word3Letter1 = "s";
        final String word3Letter2 = "t";
        final String word3Letter3 = "o";
        final String word3Letter4 = "p";
        final String word3 = "stop";

        //fault counter
        final int faultCounter = 8;

        String randomWord = word+randomInt;

        //looks for the game ----------------------------------------------------------------------------------

        JFrame hangPanel = new JFrame("Hang Game");
        hangPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        hangPanel.setLocationRelativeTo(null);

        JPanel northContainer = new JPanel();
        northContainer.setVisible(true);
        northContainer.setBackground(Color.gray);

        JPanel southContainer = new JPanel();
        southContainer.setVisible(true);
        southContainer.setBackground(Color.gray);


        final JEditorPane outComunication = new JEditorPane();
        outComunication.setVisible(true);
        outComunication.setBackground(Color.lightGray);
        outComunication.setEditable(false);
        outComunication.setSize(300,50);

        final JEditorPane counterOutComunication = new JEditorPane();
        counterOutComunication.setVisible(true);
        counterOutComunication.setBackground(Color.lightGray);
        counterOutComunication.setEditable(false);
        counterOutComunication.setSize(90,50);
        counterOutComunication.setText("Tries left: " + faultCounter);


        JPanel wordLayer = new JPanel();
        wordLayer.setVisible(true);
        wordLayer.setBackground(Color.lightGray);

        final JTextField wordLayerLetter1 = new JTextField(2);
        wordLayerLetter1.setVisible(true);
        wordLayerLetter1.setEditable(false);

        final JTextField wordLayerLetter2 = new JTextField(2);
        wordLayerLetter2.setVisible(true);
        wordLayerLetter2.setEditable(false);

        final JTextField wordLayerLetter3 = new JTextField(2);
        wordLayerLetter3.setVisible(true);
        wordLayerLetter3.setEditable(false);

        final JTextField wordLayerLetter4 = new JTextField(2);
        wordLayerLetter4.setVisible(true);
        wordLayerLetter4.setEditable(false);

        final JButton nextWord = new JButton("Next Word");
        nextWord.setVisible(false);

        final JTextField guessField = new JTextField(10);
        guessField.setVisible(true);

        //Functionality of the game --------------------------------------------------------------------------

        guessField.addActionListener(
            new ActionListener()
            {
                public int innerFaultCounter = faultCounter;
                public void actionPerformed(ActionEvent event)
                {
                    String enteredText = guessField.getText();
                    guessField.setEditable(true);

                    if(enteredText.equals(word1Letter1)) 
                    {
                        outComunication.setText("The letter \"" + enteredText + "\" is correct");
                        wordLayerLetter1.setText(word1Letter1);
                        guessField.setText("");

                    } else if(enteredText.equals(word1Letter2)) {
                        outComunication.setText("The letter \"" + enteredText + "\" is correct");
                        wordLayerLetter2.setText(word1Letter2);
                        guessField.setText("");

                    } else if(enteredText.equals(word1Letter3)) {
                        outComunication.setText("The letter \"" + enteredText + "\" is correct");
                        wordLayerLetter3.setText(word1Letter3);
                        guessField.setText("");

                    } else if(enteredText.equals(word1Letter4)) {
                        outComunication.setText("The letter \"" + enteredText + "\" is correct");
                        wordLayerLetter4.setText(word1Letter4);
                        guessField.setText("");

                    } else if(enteredText.equals(word1)) {
                        outComunication.setText("The word \"" + enteredText + "\" is correct");
                        wordLayerLetter1.setText(word1Letter1);
                        wordLayerLetter2.setText(word1Letter2);
                        wordLayerLetter3.setText(word1Letter3);
                        wordLayerLetter4.setText(word1Letter4);
                        guessField.setText("");

                    } else if(enteredText.equals("")) {
                    outComunication.setText("Please type something");
                    guessField.setText("");

                    } else {
                        outComunication.setText("The letter/Word \"" + enteredText + "\" is wrong");
                        guessField.setText("");

                        innerFaultCounter = innerFaultCounter - 1;
                        counterOutComunication.setText("Tries left: " + innerFaultCounter);

                    }

                    //make an if statement that sets the correct word if tries = 0

                    if(innerFaultCounter == 0) {
                        guessField.setEditable(false);

                        nextWord.setVisible(true);
                        innerFaultCounter = faultCounter;
                        counterOutComunication.setText("Tries left: " + innerFaultCounter);
                        outComunication.setText("The correct word is \"" + word1 + "\"");

                    }
                    String letterWordLayerLetter1 = wordLayerLetter1.getText();
                    String letterWordLayerLetter2 = wordLayerLetter2.getText();
                    String letterWordLayerLetter3 = wordLayerLetter3.getText();
                    String letterWordLayerLetter4 = wordLayerLetter4.getText();
                    if(letterWordLayerLetter1.equals(word1Letter1)) {
                        if(letterWordLayerLetter2.equals(word1Letter2)) {
                            if(letterWordLayerLetter3.equals(word1Letter3)) {
                                if(letterWordLayerLetter4.equals(word1Letter4)) {
                                    nextWord.setVisible(true);
                                    guessField.setEditable(false);
                                    innerFaultCounter = faultCounter;
                                    counterOutComunication.setText("Tries left: " + innerFaultCounter);
                                    outComunication.setText("The word \"" + word1 + "\" is correct");
                                }
                            }
                        }
                    }
                }
            }
            );

        nextWord.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                guessField.setEditable(true);
                nextWord.setVisible(false);
                wordLayerLetter1.setText("");
                wordLayerLetter2.setText("");
                wordLayerLetter3.setText("");
                wordLayerLetter4.setText("");
                outComunication.setText("");
            }
        }
        );


        //Sorting the pecies of the program intothe display

        wordLayer.add(wordLayerLetter1);
        wordLayer.add(wordLayerLetter2);
        wordLayer.add(wordLayerLetter3);
        wordLayer.add(wordLayerLetter4);
        wordLayer.add(nextWord);

        northContainer.add(guessField, BorderLayout.NORTH);
        northContainer.add(wordLayer, BorderLayout.SOUTH);

        southContainer.add(outComunication, BorderLayout.EAST);
        southContainer.add(counterOutComunication, BorderLayout.WEST);

        hangPanel.add(southContainer, BorderLayout.SOUTH);
        hangPanel.add(northContainer, BorderLayout.NORTH);
        hangPanel.pack();
        hangPanel.setVisible(true);
    }
}

现在唯一的相关部分是第一堆线,剩下的就是整个游戏功能,我没有在actionListenerrandomWord中设置if语句,因为我不会这样做直到randomWord正常工作,我怎么能解决这个问题?

3 个答案:

答案 0 :(得分:4)

String[] myWords = { "alpha", "beta", "meringue" };

String randomWord = myWords[random.nextInt(myWords.length)];

https://en.wikipedia.org/wiki/Array_data_structure

答案 1 :(得分:1)

以下代码无法编译的原因是因为您从未创建任何名为word的对象。

String word = "someRandomWordHere";
String randomWord = word + randomInt;

以下是解决此问题的方法:

1.) Turn your 3 final word variables into arrays.
2.) Loop through the arrays.
3.) Implement random in such a way that it picks one of the 3 words you've specified.
4.) Set your randomWord equal to the newly randomized word.
5.) Continue 

编辑:

public static void main(String[] args) {

    Random r = new Random();
    int randomInt = r.nextInt(3);

    String words[] = { "lude", "lose", "stop" };
    String randomWord = words[randomInt];

    System.out.println(randomWord);
}

答案 2 :(得分:0)

你应该把所有的话都放在这样的数组中:

 String words = {"word1","word2","word3"};

创建随机生成器对象

Random randomWordGenerator = new Random();

每当你想得到一个随机字时,只需生成一个随机整数,并使用这个值作为字母数组中的索引值,如下所示:

int index = randomWordGenerator.nextInt(words.length);

现在这应该给你一个随机的词:

String randomWord = words[index];