gpt4 book ai didi

java - 如何创建一个组合两个变量的变量?

转载 作者:行者123 更新时间:2023-12-01 17:05:38 26 4
gpt4 key购买 nike

我尝试在网上查找,但没有罚款任何有用的东西,我正在制作一个刽子手游戏,我想要一个字符串,它可以从一大堆单词中随机获取一个单词,我想如果我将所有单词变量设置为word1,并创建一个等于 word 加随机整数的字符串 Ex。 String randomWord = word+randomInt; 然后制作了一个随机数生成器,它将生成一个随机数并将其设置为 randomInt,但是当我这样做时,它告诉我 word 不是变量,我尝试将'这些东西围绕着整个声明,但它不起作用,我怎么能做这样的事情呢?

代码:

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);
}
}

现在唯一相关的部分是第一行,剩下的就是整个游戏功能,我没有将 actionListener 中的 if 语句设置为 randomWord ,因为在 randomWord 正常工作之前我不会这样做,我该如何解决这个问题?

最佳答案

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

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

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

关于java - 如何创建一个组合两个变量的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25715696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com