我制作的TicTacToe游戏在某种程度上起作用,我仍然在解决程序中的最后几个问题。我想添加3个客户异常类,包括IncorrectEntryException,PlayerOutOfTurnsException和StalemateException。当用户在游戏界面中输入X或O以外的字符时,必须抛出IncorrectEntryException;当用户在系统期望和O时尝试播放X时必须抛出PlayerOutOfTurnException(反之亦然);当所有游戏单元都已满并且没有实现三行时,必须抛出StalemateException。
我将为我的程序添加代码,它有两个类,GameGUI和GameLogic。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class GameGUI extends JFrame implements ActionListener {
public JTextField b1, b2, b3, b4, b5, b6, b7, b8, b9;
JPanel p, p2;
JButton check;
GameGUI() {
// create components
Container c = getContentPane();
p = new JPanel();
p2 = new JPanel();
check = new JButton("Check game.");
check.addActionListener(this);
b1 = new JTextField(null, 1);
b2 = new JTextField(null, 1);
b3 = new JTextField(null, 1);
b4 = new JTextField(null, 1);
b5 = new JTextField(null, 1);
b6 = new JTextField(null, 1);
b7 = new JTextField(null, 1);
b8 = new JTextField(null, 1);
b9 = new JTextField(null, 1);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p2.add(p);
p2.add(check);
p.setLayout(new GridLayout(3,3));
c.add(p2);
setSize(150, 150);
setVisible(true);
}
public static void main(String[] args) {
new GameLogic();
}
public void actionPerformed(ActionEvent e) {
}
}
断裂
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.IOException;
public class GameLogic extends GameGUI implements ActionListener {
int counter; // count turns
String letter = "";
boolean winner = false;
GameLogic() {
}
public static void main(String[]args) {
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==check) {
// Horizontal wins.
if(b1.getText().equals(b2.getText()) && b2.getText().equals(b3.getText()) && !(b1.getText().equals(null))) {
winner = true;
letter = b1.getText();
}
else if(b4.getText().equals(b5.getText()) && b5.getText().equals(b6.getText()) && !(b4.getText().equals(null))) {
winner = true;
letter = b4.getText();
}
else if(b7.getText().equals(b8.getText()) && b8.getText().equals(b9.getText()) && !(b7.getText().equals(null))) {
winner = true;
letter = b4.getText();
}
// Diagonal wins.
else if(b1.getText().equals(b5.getText()) && b5.getText().equals(b9.getText()) && !(b1.getText().equals(null))) {
winner = true;
letter = b1.getText();
}
else if(b3.getText().equals(b5.getText()) && b5.getText().equals(b7.getText()) && !(b3.getText().equals(null))) {
winner = true;
letter = b3.getText();
}
// Vertical wins.
else if(b1.getText().equals(b4.getText()) && b4.getText().equals(b7.getText()) && !(b1.getText().equals(null))) {
winner = true;
letter = b1.getText();
}
else if(b2.getText().equals(b5.getText()) && b5.getText().equals(b8.getText()) && !(b2.getText().equals(null))) {
winner = true;
letter = b2.getText();
}
else if(b3.getText().equals(b6.getText()) && b6.getText().equals(b9.getText()) && !(b3.getText().equals(null))) {
winner = true;
letter = b3.getText();
}
else {
winner = false;
}
if(winner == true) {
JOptionPane.showMessageDialog(null, "Winner is " + letter);
b1.setText("");
b2.setText("");
b3.setText("");
b4.setText("");
b5.setText("");
b6.setText("");
b7.setText("");
b8.setText("");
b9.setText("");
}
else if(winner == false) {
JOptionPane.showMessageDialog(null, "Stalemate!!");
b1.setText("");
b2.setText("");
b3.setText("");
b4.setText("");
b5.setText("");
b6.setText("");
b7.setText("");
b8.setText("");
b9.setText("");
}
}
}
}
如果我不清楚,我很抱歉,英语不是我的母语。我需要学习如何创建和添加这些异常类。
答案 0 :(得分:0)
根据您的评论,您想要创建自定义异常类。这很简单,只需创建一个扩展Exception
类的公共类,如下所示:
public IncorrectEntryException extends Exception {
public IncorrectEntryException() { }
public IncorrectEntryException(String message) {
super(message);
}
}
现在您可以按如下方式使用它:
抛出自定义异常:
throw new IncorrectEntryException();
抓住自定义例外:
try {
//Code
}
catch(IncorrectEntryException e) {
//Do something with your exception
}
您可以类似地创建其他异常类。有一些例如Checked和Unchecked异常,但在你的情况下,我认为这应该足够了。希望它可以帮到你。
答案 1 :(得分:0)
要创建custom exception classes
,您可以根据Exception
类型扩展RuntimeException
课程或checked/unchecked exceptions
课程。
例如,如果你想创建一个unchecked exception
NewRuntimeException,那么就可以这样做。
class NewRuntimeException extends RuntimeException {
public NewRuntimeException() {
super();
}
}