Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “线程中的异常”;“主要”;需要快速解决吗_Java_Exception - Fatal编程技术网

Java “线程中的异常”;“主要”;需要快速解决吗

Java “线程中的异常”;“主要”;需要快速解决吗,java,exception,Java,Exception,我需要为河内塔游戏编写这段代码,我有一些部分是给定的,因此它们必须是那样(我不一定同意,但无论如何),我用匈牙利语工作,所以一些单词也是匈牙利语,但我不认为它们会给理解代码带来问题。 我得到的错误是: 编辑:添加了一些注释以查找错误“我的坏” Exception in thread "main" EmptyTowerException at HanoiTower.pop(HanoiTower.java:49) at HanoiSimulator.move(HanoiSimulat

我需要为河内塔游戏编写这段代码,我有一些部分是给定的,因此它们必须是那样(我不一定同意,但无论如何),我用匈牙利语工作,所以一些单词也是匈牙利语,但我不认为它们会给理解代码带来问题。 我得到的错误是: 编辑:添加了一些注释以查找错误“我的坏”

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));
    }

}
导入java.util.array;
类EmptyTowerException扩展了异常{
}
类InvalidMoveException扩展异常{
}
公营汉诺通{
私人int magassag=10;
private int[]Torony=新int[magassag];
公共国际科隆赞酒店;
@凌驾
公共字符串toString(){
返回“HanoiTower{”+
“magassag=“+magassag+
“,Torony=“+Arrays.toString(Torony)+
“,korongSzam=“+korongSzam+
'}';
}
公共汉诺通电力公司(){
for(int i=0;i0){
梅雷特=托罗尼[i];
Torony[i]=0;
打破
}
}
if(meret==0){
抛出新的EmptyTowerException();//错误1
}
否则{
返回梅雷特;
}
}
公共void put(int size)引发InvalidMoveException{
for(int i=0;i
我在您的代码中发现了问题。 应该是:

i > -1
代替

i < -1

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

@MarsAtomic我想这就是例外情况的全文。@Andy Turner是的,我知道他在做什么。@MarsAtomic我的不好,我添加了一些带有错误的评论,但你也必须明白,我只是一个受教育不够的学生,我受的教育足够多,大学里没有人能够或试图帮助我(包括老师).以我所掌握的知识,我不可能自己解决这个问题。从
A.pop()
中捕获异常,并返回false作为handler.omg不敢相信我错过了,我已经坐了一个小时了,但我太累了,谢谢你
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; 
    }
}