怎样用java代码实现上述功能
时间: 2024-03-07 14:46:26 浏览: 188
好的,下面是一份简单的Java代码示例,实现了成语接龙游戏的娱乐模式功能。在这个示例中,我们使用了一个预定义的成语库,并使用Java Swing库构建了一个简单的GUI界面。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class IdiomGame extends JFrame implements ActionListener {
private static final String[] idioms = {"风和日丽", "东山再起", "半途而废", "大言不惭", "百年树人", "画蛇添足", "口是心非", "老牛舐犊", "人山人海", "十年树木,百年树人"};
private static final int MAX_ROUND = 10;
private static final int MAX_HINT = 3;
private JLabel infoLabel;
private JLabel scoreLabel;
private JTextField inputField;
private JButton submitButton;
private JButton hintButton;
private Random random = new Random();
private int round = 0;
private int score = 0;
private String currentIdiom;
private int hintCount = MAX_HINT;
public static void main(String[] args) {
new IdiomGame().setVisible(true);
}
public IdiomGame() {
super("成语接龙游戏");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
infoLabel = new JLabel();
scoreLabel = new JLabel("得分: " + score);
inputField = new JTextField();
inputField.addActionListener(this);
submitButton = new JButton("提交");
submitButton.addActionListener(this);
hintButton = new JButton("提示(" + hintCount + ")");
hintButton.addActionListener(this);
JPanel topPanel = new JPanel(new GridLayout(2, 1));
topPanel.add(infoLabel);
topPanel.add(scoreLabel);
JPanel bottomPanel = new JPanel(new GridLayout(1, 3));
bottomPanel.add(inputField);
bottomPanel.add(submitButton);
bottomPanel.add(hintButton);
add(topPanel, BorderLayout.NORTH);
add(bottomPanel, BorderLayout.SOUTH);
startNewRound();
}
private void startNewRound() {
round++;
infoLabel.setText("第" + round + "轮,请接:" + currentIdiom);
inputField.setText("");
hintCount = MAX_HINT;
hintButton.setText("提示(" + hintCount + ")");
currentIdiom = idioms[random.nextInt(idioms.length)];
}
private void showHint() {
hintCount--;
hintButton.setText("提示(" + hintCount + ")");
if (hintCount == 0) {
hintButton.setEnabled(false);
}
String hint = currentIdiom.substring(currentIdiom.length() - 1);
inputField.setText(hint);
inputField.requestFocus();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton || e.getSource() == inputField) {
String input = inputField.getText().trim();
if (input.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入成语!");
return;
}
if (input.charAt(0) != currentIdiom.charAt(currentIdiom.length() - 1)) {
JOptionPane.showMessageDialog(this, "回答错误!");
startNewRound();
return;
}
score++;
scoreLabel.setText("得分: " + score);
if (round == MAX_ROUND) {
JOptionPane.showMessageDialog(this, "游戏结束!得分:" + score);
System.exit(0);
}
startNewRound();
} else if (e.getSource() == hintButton) {
showHint();
}
}
}
```
在这个示例中,我们使用了一个字符串数组来存储预定义的成语库。在构造函数中,我们创建了一个包含文本标签、得分标签、文本输入框和两个按钮的GUI界面。在每个回合中,程序会从成语库中随机选择一个成语,并将其作为当前成语,然后在信息标签中提示用户接下来该回答什么成语。用户可以通过文本输入框输入他们的答案,并通过“提交”按钮提交它。如果答案是正确的,程序将增加用户的得分,并开始下一轮。如果答案是错误的,程序将重新开始当前回合。如果用户需要提示,他们可以点击“提示”按钮,程序将显示当前成语的最后一个字符,并减少提示计数器的值。
请注意,这只是一个简单的示例,实现了成语接龙游戏的娱乐模式。如果你想要实现更复杂的功能,如挑战模式或更高级的提示系统,你需要编写更复杂的代码。
阅读全文