gpt4 book ai didi

java - 为什么我收到 "symbol not found"错误?

转载 作者:行者123 更新时间:2023-12-01 10:53:28 25 4
gpt4 key购买 nike

我正在尝试编译我的主类,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;
}
}

最佳答案

假设您将两个类(Halloween 和 TreatHouse)保存在不同的 java 文件中,如果 java 编译器无法找到类文件(在您的情况下 TreatHouse),通常会出现“Symbol not find”异常

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

如果您在同一个 java 文件中拥有这两个类,

从其中一个类中删除公共(public)访问修饰符,因为我们在一个java文件中只能有一个公共(public)类,并删除中间的导入语句并使用公共(public)类名保存文件

例如。

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

class TreatHouse
{
//code
}

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

关于java - 为什么我收到 "symbol not found"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33728667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com