2015-11-16 127 views
0

我想编译我的主类Halloween,但是我收到一个错误消息,说找不到类TreatHouse。我不确定为什么会发生这种情况,但是有人能帮助我吗?此外,请随时评论您在我的代码中可能看到的任何其他错误;任何帮助是极大的赞赏。谢谢!为什么我会收到“找不到符号”错误?

import java.util.ArrayList; 
import java.util.Random; 
import java.util.Scanner; 

public class Halloween { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System.in); 
     ArrayList<TreatHouse> houses = new ArrayList<TreatHouse>(); 

     Random gen = new Random(System.currentTimeMillis()); 
     int numHouse = gen.nextInt(10) + 1; 

     // create houses 
     for (int i = 0; i < numHouse; i++) { 
      System.out.println("House " + i); 
      System.out.println("Which candy should we give out first? Candy in pot 1 or candy in pot 2?"); 
      int candyPot = scan.nextInt(); 

      System.out.println("How much candy did we buy?"); 
      int totalCandy = scan.nextInt(); 

      houses.add(new TreatHouse(candyPot, totalCandy)); 
     } 

     boolean allDone = false; 
     while(!allDone){ 
      //Each house will pass out candy for a bit. 
      for (TreatHouse house : houses) { 
       house.passTime();    
      } 

      //Check to see if all the house are done. If so set sentinel flag to true and exit while loop 
      allDone = false; 
      for(TreatHouse house: houses){ 
       //If a single house is not done then all are not done 
       if(!house.isDone()){ 
        allDone = false; 
        break; 
       } 
       else 
        allDone = true; 
      } 

      //Call static method to display some statistics about the holiday so far 


     } 
     System.out.println("All houses are done"); 
     scan.close(); 
    } 
} 

import java.util.Random; 
import java.util.Scanner; 

public class TreatHouse { 
    private int candyPot1; // amount of candy in pot 1 for a single TreatHouse 
    private int candyPot2; // amount of candy in pot 2 for a single TreatHouse 
    private int currentPot; // (1 or 2) current Pot for a single TreatHouse 
    private int totalCandy; // total candy for a single TreatHouse 
    private int currentTreaters; // current Treaters for a single TreatHouse 
    private boolean isDone = false; // is this house done for the night? 
    private int treatsPerTreater; //How much candy to give out to each treater at a single TreatHouse 
    private int nextID; 

    //Static variables 
    public static int participatingHouses = 0; //This contains the total number of houses participating in this year's holiday 
    public static int totalCandyPassedOut = 0; //This contains the total number of candy passed out for all the houses. 
    public static int totalTricksGiven = 0; //This contains the total number of tricks given out. Remember that each kid gets tricked. 
    public static int houseID = 1;    //This is the number for the house which is unique. 

    //Constructor 
    public TreatHouse() 
    { 
     participatingHouses++; 
     totalCandyPassedOut++; 
     totalTricksGiven++; 
     nextID=houseID++; 
    }  
    //Method to display the statistic variables 
    public static void getHalloweenStatus() 
    { 
     System.out.println("Total participating houses: " + participatingHouses); //Displays the total number of participating houses 
     System.out.println("Total candy passed out: " + totalCandyPassedOut);  //Displays the total amount of candy passed out 
     System.out.println("Total tricks given: " + totalTricksGiven);   //Displays the total amount of tricks given 
     System.out.println(houseID); 
    } 

    public TreatHouse(int candyPot, int totalCandy) { 
     // Error Check and split candy 
     if (totalCandy > 0) { 
      candyPot1 = totalCandy/2; 
      candyPot2 = totalCandy/2 + totalCandy % 2; 
     } else { 
      System.out.println("We can't give out candy if we don't have any." 
        + " I think we have some from last year. Yep, we have 100 " + "" + "pieces of candy to give out."); 
      totalCandy = 100; 
      candyPot1 = totalCandy/2; 
      candyPot2 = totalCandy/2 + totalCandy % 2; 
     } 

     // currentPot Check 
     if (candyPot == 1) { 
      currentPot = 1; 
     } else if (candyPot == 2) { 
      currentPot = 2; 
     } else { 
      System.out.println("Invalid choice for pot. Only 1 or 2. Using pot 1"); 
      currentPot = 1; 
     } 

     //Adjust Static variables as needed here: 


    } 

    public void passTime() { 
     Scanner scan = new Scanner(System.in); 

     //before 
     this.getCandyStatus(); 

     System.out.println("How much candy per treater should we give out?"); 
     this.setTreatsPerTreater(scan.nextInt()); 

     System.out.println("Knock, knock...." + "Trick or treat!"); 
     this.knockKnock(); 
     this.passOutCandy(); 

     //After 
     this.getCandyStatus(); 

     //Determines if the house if done for the night. 
     if(totalCandy == 0){ 
      System.out.println("Time to turn off the lights and go to bed!"); 
      System.out.println("The last candy came from pot number" + this.getLastPot()); 
      System.out.println("Happy Halloween!"); 
      isDone = true; 
     } 
    } 

    private void printCurrentPot() { 
     System.out.println("The current Pot is: " + currentPot);   
    } 



    public int getCandyCount() { 
     return candyPot1 + candyPot2; 
    } 

    public void passOutCandy() { 
     // If there are enough treats per treater for the given amount per 
     // treater, pass out candy from the current pot and switch to the other one. 
     // Else display a message that the treaters have been tricked... (no 
     // candy for them.) but do not change the current pot 

     if ((currentPot == 1 && candyPot1 == 0) || (currentPot == 2 && candyPot2 == 0)) { 
      // Switch pots and print message 
      System.out.println("There is no candy in the currentPot Switching pots."); 
      switchPots(); 
     } 

     {// There is some candy to give out. 
      int currentPotAmmount = getCurrentPotAmount(); 
      if (treatsPerTreater * currentTreaters <= currentPotAmmount) { 
       //Passing out Candy 
       if (currentPot == 1) { 
        candyPot1 -= treatsPerTreater * currentTreaters; 
        totalCandy -= treatsPerTreater * currentTreaters; 
       } else { 
        candyPot2 -= treatsPerTreater * currentTreaters; 
        totalCandy -= treatsPerTreater * currentTreaters; 
       } 
       switchPots(); 
      } else { 
       // trick 
       System.out.println("You have been tricked!"); 
      } 

     } 

    } 

    private int getCurrentPotAmount() { 
     if (currentPot == 1) 
      return candyPot1; 
     else 
      return candyPot2; 
    } 

    private void switchPots() { 
     if (currentPot == 1) { 
      currentPot = 2; 
     } else 
      currentPot = 1; 

    } 

    // Sets the number of trick or treaters. 
    public void knockKnock() { 
     Random gen = new Random(System.currentTimeMillis()); 
     this.currentTreaters = gen.nextInt(13) + 1; // 1 to 13 treaters. 
     System.out.println("There are " + currentTreaters + " treaters."); 
    } 

    // Displays how much candy in each pot, total candy left 

    public void getCandyStatus() { 
     this.printCurrentPot(); 
     System.out.println("Candy in Pot 1: " + candyPot1); 
     System.out.println("Candy in Pot 2: " + candyPot2); 

    } 

    // returns the pot number for which candy was last given. 
    public int getLastPot() { 
     if (currentPot == 1) 
      return 2; 
     else { 
      return 1; 
     } 
    } 

    public void setTreatsPerTreater(int treatsPerTreater) { 
     this.treatsPerTreater = treatsPerTreater; 
    } 

    public boolean isDone() { 

     return isDone; 
    } 
} 
+0

我可以说你宣布了​​“TreatHouse”两次(至少在帖子中)。 –

+0

你也不能在一个文件中有2个公共类。 – zubergu

回答

0

你似乎有你的班上有两个副本...一半,你有另一套进口的(这永远只能是在文件的顶部)和另一TreatHouse类...

将代码粘贴到IDE中并删除后半部分后,我没有编译错误。

+1

那么你还有两个类,它们需要分离成它们自己的文件,Halloween.java和TreatHouse.java 代码本身没有编译错误。 – Numeron

+0

...实际上TreatHouse仍然在那里两次 – Numeron

+0

好吧,我删除了第二个“TreatHouse”。然而,'Halloween'类是'TreatHouse'的驱动类,而我在编译'Halloween'时遇到问题,因为它表示找不到符号“TreatHouse”。 “TreatHouse”虽然编译得很好。 – salmon1407

0

假设您已经保存了两个类(万圣节和TreatHouse)在不同的java文件,通常是“符号未找到”的异常会来,如果Java编译器无法找到类文件(在你的情况TreatHouse)

如果两个java文件在同一个文件夹中,那么当您编译Halloween.java时,它会自动编译TreatHouse.java,如果java文件位于不同的文件夹中,则需要先编译TreatHouse.java并将类路径设置为TreatHouse.class位置稍后编译Halloween.java

如果你在同一个java文件中有两个类,

从类之一中删除公共访问修饰符,因为我们可以在java文件中只有一个公共类,并删除中间的导入语句并使用公共班级名称

例如。

import java.util.ArrayList; 
import java.util.Random; 
import java.util.Scanner; 
public class Halloween 
{ 
//code 
} 

class TreatHouse 
{ 
//code 
} 

上面的代码的文件名应该是Halloween.java 编译Halloween.java(javac的Halloween.java)后,你会得到两个类文件Halloween.class和TreatHouse.class

+0

问题解决!谢谢! – salmon1407

相关问题