Vinjett 3, Period 2
Felsökning och felhantering
Hej! Jag fick följande ofångade undantag när jag körde programmet med
n = 3 (jag bifogar båda tracen och koden). Jag hittar inte felet och skulle vara
tacksam om du kunde hjälpa till !
Exception in thread " main " java . util . E m p t y S t a c k E x c e p t i o n
at java . util . Stack . peek ( Stack . java :85)
at java . util . Stack . pop ( Stack . java :67)
at Hanoi . moveB2C ( Hanoi . java :60)
at Hanoi . moveA2C ( Hanoi . java :52)
at Driver . main ( Driver . java :8)
36
37
38
39
40
41
42
43
44
45
46
void moveA2B ( int n ){
System . out . println ( " moveA2B : calling moveA2B with " + n + " \ nCurrent stacks : " + this );
if (n >0){
moveA2C (n -1);
System . out . println ( " moveA2B : after calling moveA2C with " + (n -1) + " \ nCurrent stacks : " + this );
C . push ( A . pop ());
System . out . println ( " moveA2B : after moving one element " );
moveC2B (n -1);
System . out . println ( " moveA2B : after calling moveC2B with " + (n -1) + " \ nCurrent stacks : " + this );
}
}
48
49
50
51
52
53
54
void moveA2C ( int n ){
if (n >0){
moveA2B (n -1);
C . push ( A . pop ());
moveB2C (n -1);
}
}
56
57
58
59
60
61
62
63
64
void moveB2C ( int n ){
if (n >0){
moveB2A (n -1);
System . out . println ( this );
C . push ( B . pop ());
System . out . println ( this );
moveA2C (n -1);
}
}
66
67
68
69
70
71
72
73
74
void moveC2A ( int n ){
if (n >0){
moveC2B (n -1);
System . out . println ( this );
A . push ( C . pop ());
System . out . println ( this );
moveB2A (n -1);
}
}
76
77
78
79
80
81
82
83
84
void moveB2A ( int n ){
if (n >0){
moveB2C (n -1);
System . out . println ( this );
A . push ( B . pop ());
System . out . println ( this );
moveC2A (n -1);
}
}
87
88
89
90
91
92
93
94
95
96
97
void moveC2B ( int n ){
if (n >0){
moveC2A (n -1);
System . out . println ( this );
B . push ( C . pop ());
System . out . println ( this );
moveA2B (n -1);
}
}
4
5
6
7
8
9
10
11
}
public class Driver {
public static void main ( String [] args ){
Hanoi h = new Hanoi (3);
h . moveA2C (3);
}
}
2