2016-10-18 17 views
1

我有三类:Mazesolver,HexagonMaze。当我尝试在Mazesolver类中创建一个Hexagon对象时,会发生错误。任何人都可以请帮我解决这个问题吗?此外,在迷宫中获得对开始Hexagon的引用意味着什么?当参数超出范围时,如何在另一个类中创建对象?

public class Hexagon extends HexComponent 
{ 
    // constants 
    private static final Color WALL_COLOR = Color.BLACK; 
    private static final Color START_COLOR = Color.GREEN; 
    private static final Color END_COLOR = Color.YELLOW; 
    private static final Color UNVISITED_COLOR = Color.CYAN; 
    private static final Color PROCESSED_COLOR = Color.BLUE; 
    private static final Color PUSHED_COLOR = Color.MAGENTA; 
    private static final Color END_PROCESSED_COLOR = Color.RED; 
    private static final Color START_PROCESSED_COLOR = Color.PINK; 

    //enum to represent available hexagon types 
    public static enum HexType{WALL, START, END, UNVISITED, PROCESSED, PUSHED,  END_PROCESSED, START_PROCESSED}; 

    // Attributes 
    private HexType type; // Stores the type of Hexagon this currently is 
    private boolean isStart; // Is this the start? 
    private boolean isEnd; // Is this the end? 
    private Hexagon[] neighbors; // Stores the hexagons which surround this one on each of 6 sides 

    /** 
    * Create a Hexagon tile of the specified type 
    * @param t the HexType to create 
    */ 
    public Hexagon(HexType t) { 
     this.type = t; 
     this.isStart = t == HexType.START; 
     this.isEnd = t == HexType.END; 

     //set the initial color based on the initial type 
     this.setColor(this.type); 
     //allocate space for the neighbor array 
     this.neighbors = new Hexagon[6]; 
    } 

如何在MazeSolver中创建一个Hexagon对象?

public class MazeSolver 
{ 
    public static void main (String[] args) { 
     try { 
      if (args.length < 1) { 
       throw new IllegalArgumentException("No Maze Provided"); 
      } 
      String maze0 = args[0]; 
      private ArrayStack<String> steps; 
      Hexagon Start = new Hexagon(t); //error 
     } 
+0

你的'ArrayStack'实例'steps'具有非法修饰符。你不能在方法中的局部范围变量上使用'private'。此外,您未包含任何有关您所看到的错误类型的详细信息。请将这些详细信息添加到您的帖子。因为,不可能说出哪些错误是由于错误的复制/粘贴与实际问题所致。 – nbrooks

回答

3

我没有编码大师,但也可能是因为只有Hexagon构造函数需要你传递一个HexType值。我可能是错的,但是我认为问题在于,当t不是HexType值时,您将t传入您的六角形构造函数中。您需要将其中的一个传递给您的Hexagon构造函数:HexType.WALL, HexType.START, HexType.END, HexType.UNVISITED, HexType.PROCESSED, HexType.PUSHED, HexType.END_PROCESSED, HexType.START_PROCESSED

编辑:所以是我认为可以肯定地说你只需要通过HexType.VALUE到你的Hexagon构造函数中,VALUE是你的HexType枚举类的任何值。

我是新来的StackOverFlow,请让我知道如果我错了,所以我可以删除我的答案或至少纠正它。

+0

是的,@LeonelRamos我相信这是问题所在。 – gschambial

+0

@TedLaw你还可以发布你的错误的StackTrace。 – gschambial

+0

当我将T更改为START时,它说START不能解析为变量。 –

0
 Hexagon Start = new Hexagon(t); //error 

向上述线一切是正确的,除了:

1)它可以是没有限定,在使用它之前,首先定义为六边形。 2)它可能是你已经定义了t某处代码,但必须是Hexagon,将其类型更改为Hexagon。 3)如果你在两点之上都做得正确,那么你必须将Hexagon类导入MazeSolver。 lke:

import <Package>.HEXAGON; 
public class MazeSolver 
{ 
    public static void main (String[] args) { 
     try { 
      if (args.length < 1) { 
       throw new IllegalArgumentException("No Maze Provided"); 
      } 
      String maze0 = args[0]; 
      private ArrayStack<String> steps; 
      HexType t; // Change Here 
      Hexagon Start = new Hexagon(t); // No Error 
+0

当我将T更改为HEX时 –

+0

当我将T更改为HEXTYPE时。它说不能解决变量。现在该怎么办 –

+0

@TedLaw你知道什么是HexType,请分享关于HexType –