我正在创建一个简单的基于文本的河内塔游戏,供用户玩。程序应询问用户从哪个钉子移到哪个钉子。我必须为此程序使用堆栈。我还必须实现这些给定的方法:
这是我到目前为止的内容:
Towers.java
import java.util.*;
public class Towers
{
public class Towers {
private List<Integer> peg1;
private List<Integer> peg2;
private List<Integer> peg3;
Towers(int n){
peg1 = new LinkedList<Integer>();
peg2 = new LinkedList<Integer>();
peg3 = new LinkedList<Integer>();
}
public int countRings(int pegNumber){
if (pegNumber == 1)
{
return peg1.size();
}
if (pegNumber == 2)
{
return peg2.size();
}
if (pegNumber == 3)
{
return peg3.size();
}
return -1;
}
public int getTopDiameter(int pegNumber){
if (countRings(pegNumber) > 0)
{
if (pegNumber == 1)
{
return peg1.get(peg1.size()-1); // Return the last disc from peg1
}
if (pegNumber == 2)
{
return peg2.get(peg2.size()-1); // Return the last disc from peg2
}
if (pegNumber == 3)
{
return peg3.get(peg3.size()-1); // Return the last disc from peg3
}
}
return 0;
}
public void move(int startPeg, int endPeg)
{
int pegMove = 0; // Temporary spot to hold disc
if(startPeg == 1)
{
if(endPeg == 1)
{
pegMove = peg1.get(peg1.size() - 1); // Take the last disc from startPeg, add to discMove
peg1.remove(peg1.size() - 1);// Remove the disc from startPeg
peg1.add(pegMove); // Add disc to the endPeg
System.out.printf( "\n%d --> %d", startPeg, endPeg);
}
else if(endPeg == 2)
{
pegMove = peg1.get(peg1.size() - 1); // Take the last disc from startPeg, add to discMove
peg1.remove(peg1.size() - 1); // Remove the disc from startPeg
peg2.add(pegMove); // Add disc to the endPeg
System.out.printf( "\n%d --> %d", startPeg, endPeg);
}
else if(endPeg == 3)
{
pegMove = peg1.get(peg1.size() - 1); // Take the last disc from startPeg, add to discMove
peg1.remove(peg1.size() - 1); // Remove the disc from startPeg
peg3.add(pegMove); // Add disc to the endPeg
System.out.printf( "\n%d --> %d", startPeg, endPeg);
}
}
if(startPeg == 2)
{
if(endPeg == 1)
{
pegMove = peg2.get(peg2.size() - 1); // Take the last disc from startPeg, add to discMove
peg2.remove(peg2.size() - 1);// Remove the disc from startPeg
peg1.add(pegMove); // Add the disc to the endPeg
System.out.printf( "\n%d --> %d", startPeg, endPeg);
}
else if(endPeg == 2)
{
pegMove = peg2.get(peg2.size() - 1); // Take the last disc from startPeg, add to discMove
peg2.remove(peg2.size() - 1); // Remove the disc from startPeg
peg2.add(pegMove); // Add the disc to the endPeg
System.out.printf( "\n%d --> %d", startPeg, endPeg);
}
else if(endPeg == 3)
{
pegMove = peg2.get(peg2.size() - 1); // Take the last disc from startPeg, add to discMove
peg2.remove(peg2.size() - 1); // Remove the disc from startPeg
peg3.add(pegMove); // Add the disc to the endPeg
System.out.printf( "\n%d --> %d", startPeg, endPeg);
}
}
if(startPeg == 3)
{
if(endPeg == 1)
{
pegMove = peg3.get(peg3.size() - 1); // Take the last disc from startPeg, add to discMove
peg3.remove(peg3.size() - 1);// Remove the disc from startPeg
peg1.add(pegMove); // Add the disc to the endPeg
System.out.printf( "\n%d --> %d", startPeg, endPeg);
}
else if(endPeg == 2)
{
pegMove = peg3.get(peg3.size() - 1); // Take the last disc from startPeg, add to discMove
peg3.remove(peg3.size() - 1); // Remove the disc from startPeg
peg2.add(pegMove); // Add the disc to the endPeg
System.out.printf( "\n%d --> %d", startPeg, endPeg);
}
else if(endPeg == 3)
{
pegMove = peg3.get(peg3.size() - 1); // Take the last disc from startPeg, add to discMove
peg3.remove(peg3.size() - 1); // Remove the disc from startPeg
peg3.add(pegMove); // Add the disc to the endPeg
System.out.printf( "\n%d --> %d", startPeg, endPeg);
}
}
}
}
由于某种原因,它无法正确编译,我无法弄清楚下一步该怎么做。
----jGRASP exec: javac -g Towers.java
Towers.java:160: error: reached end of file while parsing
}
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
最佳答案
您的课程开始于
import java.util.*;
public class Towers
{
public class Towers {
private List<Integer> peg1;
...
虽然应该仅以
import java.util.*;
public class Towers
{
private List<Integer> peg1;
...