首页 > 解决方案 > 线程“main”中的异常需要快速解决

问题描述

我需要把这段代码写到河内塔游戏中,我有一些部分是给的,所以它们必须是那样的(我不一定同意,但无论如何)我在匈牙利工作,所以有些词是匈牙利语同样,但我认为它们不会对代码的理解造成问题。我得到的错误是: 编辑:添加了一些评论以找到我的错误

Exception in thread "main" EmptyTowerException
    at HanoiTower.pop(HanoiTower.java:49)
    at HanoiSimulator.move(HanoiSimulator.java:58)
    at HanoiSimulator.main(HanoiSimulator.java:72)

这是我的代码:

import java.util.Scanner;

public class HanoiSimulator {
    public HanoiTower elso = new HanoiTower();
    public HanoiTower masodik = new HanoiTower();
    public HanoiTower harmadik = new HanoiTower();


    public HanoiSimulator(int x) throws InvalidMoveException {
        for (int i = x; i > 0; i--) {
            elso.put(x);
            x-=1;
        }

    }


    public boolean isFinished(){
        boolean win = false;
        if(masodik.korongSzam == 3)
        {
            win = true;
        }
        else{
            win = false;
        }
        return win;
    }

    public boolean move(int from, int to) throws EmptyTowerException, InvalidMoveException {
        HanoiTower A = new HanoiTower();
        HanoiTower B = new HanoiTower();
        System.out.println(elso);
        //System.out.println(A);
        if (from == 1) {
            A = elso;
            System.out.println(A);
        }
        else {
            if (from == 2) {
                A = masodik;
            }
            else {
                A = harmadik;
            }
        }
        if (to == 1) {
            B = elso;
        }
        else {
            if (to == 2) {
                B = masodik;
            }
            else {
                B = harmadik;
            }
        }
        int x = A.pop();         //ERROR 2
        B.put(x);
        return true;
    }

    public static void main(String[] args) throws InvalidMoveException, EmptyTowerException {
        HanoiSimulator simulator = new HanoiSimulator(4);
        Scanner sc=new Scanner(System.in);
        while(!simulator.isFinished()) {
            System.out.println(simulator);
            System.out.print("Which tower should I take the top disk from? (1-3) ");
            int from = sc.nextInt();
            System.out.print("On which tower should I put it down? (1-3) ");
            int to = sc.nextInt();
            if(simulator.move(from,to)) System.out.println("Move successful."); //ERROR 3
            else System.out.println("This move can not be carried out.");
        }
        System.out.println("Congrats, you win. You've just brought the end of the world. Thanks.... -.-");
    }
}

这是 HanoiTower 类:

import java.util.Arrays;

class EmptyTowerException extends Exception {

}

class InvalidMoveException extends Exception {

}

public class HanoiTower {
      private int magassag = 10;
      private int[] Torony = new int[magassag];
      public int korongSzam;

    @Override
    public String toString() {
        return "HanoiTower{" +
                "magassag=" + magassag +
                ", Torony=" + Arrays.toString(Torony) +
                ", korongSzam=" + korongSzam +
                '}';
    }

    public HanoiTower() {
        for (int i = 0; i < Torony.length; i++) {
            Torony[i] = 0;
        }

    }

    public HanoiTower(int h) {
        for (int i = 0; i < h; i++) {
            magassag = h;
            Torony[i] = h-i;
        }
    }

    public int pop() throws EmptyTowerException {
        int meret=0;
        for (int i = magassag-1; i < -1; i--) {
            if (Torony[i] > 0) {
                meret = Torony[i];
                Torony[i] = 0;
                break;
            }
        }
        if(meret == 0){
            throw new EmptyTowerException();   // ERROR 1
        }
        else {
            return meret; 
        }
    }

    public void put(int size) throws InvalidMoveException {
        for (int i = 0; i < Torony.length ; i++) {
            if(Torony[i] == 0) {
                Torony[i] = size;
                korongSzam +=1;
                break;
            }
            else
                if(Torony[i] < size)
                    throw new InvalidMoveException();
        }
        //System.out.println(Arrays.toString(Torony));
    }


    public static void main(String[] args) {
        HanoiTower test= new HanoiTower();
        try{
            test.pop();
            System.err.println("[ERROR] pop succesful from empty tower");
        } catch (EmptyTowerException e){
            System.err.println("[OK] pop unsuccesful from empty tower");
        }
        try{
            test.put(6);
            System.err.println("[OK] put 6 succesful on empty tower");
        } catch (InvalidMoveException e){
            System.err.println("[ERROR] put 6 succesful on empty tower");
        }
        try{
            test.put(4);
            System.err.println("[OK] put 4 succesful on tower [6]");
        } catch (InvalidMoveException e){
            System.err.println("[ERROR] put 4 succesful on tower [6]");
        }
        try{
            test.put(1);
            System.err.println("[OK] put 1 succesful on tower [6 4]");
        } catch (InvalidMoveException e){
            System.err.println("[ERROR] put 1 succesful on tower [6 4]");
        }
        try{
            test.put(2);
            System.err.println("[ERROR] put 2 succesful on tower [6 4 1]");
        } catch (InvalidMoveException e){
            System.err.println("[OK] put 1 succesful on tower [6 4 1]");
        }
        System.out.println(Arrays.toString(test.Torony));
    }

}

标签: javaexception

解决方案


我在您的代码中发现了问题。它应该是:

i > -1

安装的

i < -1

在这个方法中。

public int pop() throws EmptyTowerException {
    int meret=0;
    for (int i = magassag-1; i > -1; i--) {
        if (Torony[i] > 0) {
            meret = Torony[i];
            Torony[i] = 0;
            break;
        }
    }
    if(meret == 0){
        throw new EmptyTowerException();   // ERROR 1
    }
    else {
        return meret; 
    }
}

另外我建议在抛出异常时处理它并告诉用户改变他们的选择。祝你好运 :)


推荐阅读