我正在创建一个简单的基于文本的河内塔游戏供用户玩。程序应该询问用户哪个挂钩要移动,哪个挂钩要移动到哪个挂钩。我必须使用Stacks来实现这个程序。我还必须实现这些给定的方法:
这是我到目前为止所拥有的: 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.
答案 0 :(得分:2)
您的课程以
开头import java.util.*;
public class Towers
{
public class Towers {
private List<Integer> peg1;
...
虽然它应该只用
开头import java.util.*;
public class Towers
{
private List<Integer> peg1;
...