gpt4 book ai didi

java - 随机词返回 null

转载 作者:行者123 更新时间:2023-12-02 00:14:54 25 4
gpt4 key购买 nike

当我将其作为标签“JLabel lblWord = new JLabel(randomWord) 运行时,它不显示标签,但是当我作为 system.out.println(randomWord) 运行时,它给我 null...它应该返回一个文本文件中的单词,但即使我将整个代码移至底部,它似乎也不起作用

import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;

public final class Hangman extends JFrame
{
static String randomWord;
int i = 0;
static JPanel panel;
static JPanel panel2;
static JPanel panel3;
static JPanel panel4;

public static String readWord()
{
try
{
BufferedReader reader = new BufferedReader(new FileReader("Words.txt"));
String line = reader.readLine();
List<String> words = new ArrayList<String>();
while(line != null)
{
String[] wordsLine = line.split(" ");
boolean addAll = words.addAll(Arrays.asList(wordsLine));
line = reader.readLine();
}
Random rand = new Random(System.currentTimeMillis());
String randomWord = words.get(rand.nextInt(words.size()));
}catch (Exception e){

}
return randomWord;
}

public Hangman()
{

JButton[] buttons = new JButton[26];

panel = new JPanel(new GridLayout(0,9));
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();

JButton btnRestart = new JButton("Restart");
btnRestart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{

}
});

JButton btnNewWord = new JButton("Add New Word");
btnNewWord.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try
{
FileWriter fw = new FileWriter("Words.txt", true);
PrintWriter pw = new PrintWriter(fw, true);

String word = JOptionPane.showInputDialog("Please enter a word: ");

pw.println(word);
pw.close();
}
catch(IOException ie)
{
System.out.println("Error Thrown" + ie.getMessage());
}
}
});

JButton btnHelp = new JButton("Help");
btnHelp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String message = "The word to guess is represented by a row of dashes, giving the number of letters and category of the word."
+ "\nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions."
+ "\nIf the suggested letter does not occur in the word, the other player draws one element of the hangman diagram as a tally mark."
+ "\n"
+ "\nThe game is over when:"
+ "\nThe guessing player completes the word, or guesses the whole word correctly"
+ "\nThe other player completes the diagram";
JOptionPane.showMessageDialog(null,message, "Help",JOptionPane.INFORMATION_MESSAGE);
}
});

JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});

ImageIcon icon = new ImageIcon("D:\\Varsity College\\Prog212Assign1_10-013803\\images\\Hangman1.jpg");
JLabel lblWord = new JLabel(randomWord);
JLabel label = new JLabel();
label.setIcon(icon);
String b[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
for(i = 0; i < buttons.length; i++)
{
buttons[i] = new JButton(b[i]);

panel.add(buttons[i]);
}

panel2.add(label);

panel3.add(btnRestart);
panel3.add(btnNewWord);
panel3.add(btnHelp);
panel3.add(btnExit);
panel4.add(lblWord);
}

public static void main(String[] args)
{

System.out.println();
Hangman frame = new Hangman();
Box mainPanel = Box.createVerticalBox();
frame.setContentPane(mainPanel);
mainPanel.add(panel, BorderLayout.NORTH);
mainPanel.add(panel2);
mainPanel.add(panel4);
mainPanel.add(panel3);
frame.pack();
frame.setVisible(true);
}
}

最佳答案

randomWord 变量被声明了两次,一次是在 Hangman 类中,它在那里保持为 null,第二次是在 readWord 方法中,它被随机字符串填充然后返​​回。请理解,这是两个不同且完全不同的变量。还有 readWord() 内部声明的变量其范围仅限于此方法。换句话说,它根本不存在于该方法之外。

解决办法:

  • 去掉 Hangman 类中的 randomWord 变量——它比毫无值(value)更糟糕,因为它具有误导性。
  • 在需要时调用 readWord() 方法来获取随机单词。这就是您首先使用此方法的原因。
  • 仔细阅读变量作用域和变量阴影,这两个概念是您遇到问题的。
  • 我会稍微改进一下,并读取单词文件一次,将所有单词放入 ArrayList<String> 中。 。然后,当我需要一个随机单词时,我会从 ArrayList 中随机获取一个单词。无需不断重新读取同一个文件。

关于java - 随机词返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12046699/

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