我正在尝试编译我的主要课程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 :(得分:0)
你似乎有两个班级的副本......中途你有另一组导入(只能在文件的顶部)和另一个TreatHouse类......
将代码粘贴到IDE中并删除后半部分后,我没有编译错误。
答案 1 :(得分:0)
假设您已将两个类(Halloween和TreatHouse)保存在不同的java文件中,如果java编译器无法找到类文件(在您的情况下为TreatHouse),通常会出现“未找到符号”异常
如果两个java文件在同一个文件夹中,那么在编译Halloween.java时它会自动编译TreatHouse.java,如果java文件在不同的文件夹中,则需要先编译TreatHouse.java并将类路径设置为TreatHouse。类位置稍后编译Halloween.java
或
如果你在同一个java文件中同时拥有这两个类,那么
从其中一个类中删除公共访问修饰符,因为我们在java文件中只能有一个公共类,并删除中间的import语句并使用公共类名保存文件
例如
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