线程异常"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.... -.-");
}
}

这是河内塔类:

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

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

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

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

最新更新