使用多个类处理Java异常

使用多个类处理Java异常,java,oop,exception-handling,Java,Oop,Exception Handling,我需要做以下例外:如果行不在1和3之间,则为NoSuchRowException;如果获取的棍棒数不在1和3之间,则为IllegalStickException;如果获取的棍棒数在1和3之间,但大于该行中剩余的棍棒数,则为NoteNoughStickException。我的问题是我真的不懂语法如果有人能帮我开始一个例外,我想我可以找出其他例外。 到目前为止,我的主要课程是: /* * To change this template, choose Tools | Templates * a

我需要做以下例外:如果行不在1和3之间,则为NoSuchRowException;如果获取的棍棒数不在1和3之间,则为IllegalStickException;如果获取的棍棒数在1和3之间,但大于该行中剩余的棍棒数,则为NoteNoughStickException。我的问题是我真的不懂语法如果有人能帮我开始一个例外,我想我可以找出其他例外。

到目前为止,我的主要课程是:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nimapp;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author jrsullins
 */
public class NimApp extends JFrame implements ActionListener {
    private static final int ROWS = 3;
    private JTextField[] gameFields; // Where sticks for each row shown
    private JTextField rowField;     // Where player enters row to select
    private JTextField sticksField;  // Where player enters sticks to take
    private JButton playButton;      // Pressed to take sticks
    private JButton AIButton;        // Pressed to make AI's move
    private NimGame nim;

    public NimApp() {
        // Build the fields for the game play 
        rowField = new JTextField(5);
        sticksField = new JTextField(5);
        playButton = new JButton("PLAYER");
        AIButton = new JButton("COMPUTER");
        playButton.addActionListener(this);
        AIButton.addActionListener(this);
        AIButton.setEnabled(false);

        // Create the layout
        JPanel mainPanel = new JPanel(new BorderLayout());
        getContentPane().add(mainPanel);

        JPanel sticksPanel = new JPanel(new GridLayout(3, 1));
        mainPanel.add(sticksPanel, BorderLayout.EAST);

        JPanel playPanel = new JPanel(new GridLayout(3, 2));
        mainPanel.add(playPanel, BorderLayout.CENTER);

        // Add the fields to the play panel
        playPanel.add(new JLabel("Row: ", JLabel.RIGHT));
        playPanel.add(rowField);
        playPanel.add(new JLabel("Sticks: ", JLabel.RIGHT));
        playPanel.add(sticksField);
        playPanel.add(playButton);
        playPanel.add(AIButton);

        // Build the array of textfields to display the sticks
        gameFields = new JTextField[ROWS];
        for (int i = 0; i < ROWS; i++) {
            gameFields[i] = new JTextField(10);
            gameFields[i].setEditable(false);
            sticksPanel.add(gameFields[i]);
        }
        setSize(350, 150);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        nim = new NimGame(new int[]{3, 5, 7});
        draw();
    }

    // Utility function to redraw game
    private void draw() {
        for (int row = 0; row < ROWS; row++) {
            String sticks = "";
            for (int j = 0; j < nim.getRow(row); j++) {
                sticks += "|   ";
            }
            gameFields[row].setText(sticks);
        }
        rowField.setText("");
        sticksField.setText("");
    }

    public void actionPerformed(ActionEvent e) {
        // Player move
        if (e.getSource() == playButton) {
            // Get the row and number of sticks to take
            int row = Integer.parseInt(rowField.getText())-1;
            int sticks = Integer.parseInt(sticksField.getText());

            // Play that move
            nim.play(row, sticks);

            // Redisplay the board and enable the AI button
            draw();
            playButton.setEnabled(false);
            AIButton.setEnabled(true);

            // Determine whether the game is over
            if (nim.isOver()) {
                JOptionPane.showMessageDialog(null, "You win!");
                playButton.setEnabled(false);
            }
        }

        // Computer move
        if (e.getSource() == AIButton) {
            // Determine computer move
            nim.AIMove();
            // Redraw board
            draw();
            AIButton.setEnabled(false);
            playButton.setEnabled(true);

            // Is the game over?
            if (nim.isOver()) {
                JOptionPane.showMessageDialog(null, "You win!");
                playButton.setEnabled(false);
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        NimApp a = new NimApp();
    }
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
软件包nimapp;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
/**
*
*@jrsullins
*/
公共类NimApp扩展JFrame实现ActionListener{
私有静态最终整数行=3;
私有JTextField[]游戏场;//显示的每一行的位置
private JTextField rowField;//玩家在其中输入要选择的行
私有JTextField sticksField;//玩家输入棍子的位置
私人JButton playButton;//按此键可获取棍棒
private JButton AIButton;//按下此按钮可移动AI
私人游戏nim;
公共应用程序(){
//为游戏搭建场地
rowField=新的JTextField(5);
sticksField=新的JTextField(5);
playButton=新的JButton(“播放器”);
AIButton=新的JButton(“计算机”);
playButton.addActionListener(此);
AIButton.addActionListener(此);
AIButton.setEnabled(假);
//创建布局
JPanel mainPanel=newjpanel(newborderlayout());
getContentPane().add(主面板);
JPanel sticksPanel=新的JPanel(新的网格布局(3,1));
添加(StickPanel,BorderLayout.EAST);
JPanel playPanel=新的JPanel(新的网格布局(3,2));
添加(playPanel,BorderLayout.CENTER);
//将字段添加到播放面板
添加(新JLabel(“行:”,JLabel.RIGHT));
playPanel.add(rowField);
添加(新的JLabel(“Sticks:,JLabel.RIGHT”);
playPanel.add(sticksField);
添加(播放按钮);
playPanel.add(AIButton);
//构建文本字段数组以显示棍棒
gameFields=新的JTextField[行];
对于(int i=0;i
支持类:

package nimapp;
import java.util.Random;
import javax.swing.JOptionPane;
import java.io.*;
import java.lang.*;

public class NimGame { 
    int x = 1;
    int[] Sticks; //creating an array of sticks
    int totalSticks = 0;

    public NimGame(int[] initialSticks){
    Sticks = initialSticks;}

    public int getRow(int r){
        return Sticks[r];}

    public void play(int r, int s) throws IllegalSticksException {
        try {
            Sticks[r]=Sticks[r]-s;               
            if(s < 0 || s > 3)
                throw new IllegalSticksException();
        } catch (IllegalSticksException ex){
             JOptionPane.showMessageDialog(null, "Not a valid row!");
        } catch (IndexOutOfBoundsException e){
             JOptionPane.showMessageDialog(null, "Too Many Sticks!");
        }
    }





public boolean isOver(){
    int theTotal = 0;
    for (int i = 0; i< Sticks.length; i++){
         theTotal = Sticks[i];
    System.out.println(Sticks[i]);
    System.out.println(theTotal);
    }
       totalSticks = theTotal; 
       if (totalSticks <= 0){            
           return true;
        }
        else return false;
}

public void AIMove(){
    Random randomInt = new Random ();

    boolean tryRemove =  true;

    while(tryRemove && totalSticks >= 1){
        int RandomRow = randomInt.nextInt(3);
        if(Sticks[RandomRow] <= 0)//the computer can't remove from this row
            continue;

        //the max number to remove from row
        int size = 3;
        if( Sticks[RandomRow] < 3)//this row have least that 3 cards
            size = Sticks[RandomRow];//make the max number to remove from the row be the number of cards on the row 
        int RandomDiscard = randomInt.nextInt(size) + 1;
        Sticks[RandomRow] = Sticks[RandomRow] - RandomDiscard;
        //I don't know if this is needed, but since we remove a RandomDiscard amount lest decrease the totalSticks
        totalSticks = totalSticks - RandomDiscard;
        //exit loop
        tryRemove = false;
    }

     if(totalSticks <= 1){
        int RandomRow = 0;
        Sticks[RandomRow] = Sticks[RandomRow]-1;
        isOver();
    }
}

}
nimapp包;
导入java.util.Random;
导入javax.swing.JOptionPane;
导入java.io.*;
导入java.lang.*;
公共类游戏{
int x=1;
int[]棍棒;//创建棍棒数组
整数=0;
公共游戏(int[]首字母){
Sticks=初始Sticks;}
公共整数getRow(整数r){
返回棒[r];}
公共无效播放(int r,int s)抛出非法粘贴异常{
试一试{
棍棒[r]=棍棒[r]-s;
如果(s<0 | | s>3)
抛出新的非法StickException();
}捕获(非法粘贴例外){
showMessageDialog(null,“不是有效行!”);
}catch(IndexOutOfBoundsException e){
showMessageDialog(null,“棍子太多了!”);
}
}
公共布尔值isOver(){
int总=0;
for(int i=0;i
您编写的语法没有问题

问题是您在错误的位置捕获异常。您(显然)打算
play
IllegalSticksException
传播给其调用方。但是