java - 尝试在同一包中的另一个类中引用 SteppingStone5_Recipe 类时,编译器显示“找不到符号”错误消息
问题描述
尝试在同一包中的另一个类中引用 SteppingStone5_Recipe 类时,编译器显示“找不到符号”错误消息。我认为问题是因为这些类有不同的包,但是一旦我使用编译器工具重命名了包,这个解决方案就不起作用了。
这是 SteppingStone6_RecipeBox 类:
package SteppingStones;
import java.util.ArrayList;
import java.util.Scanner;
public class SteppingStone6_RecipeBox {
/**
* @param args the command line arguments
*/
private ArrayList<SteppingStone5_Recipe> listOfRecipes;
/**
* @param args the command line arguments
*/
/**
* @return the listOfRecipes
*/
public ArrayList<SteppingStone5_Recipe> getListOfRecipes() {
return listOfRecipes;
}
/**
* @param listOfRecipes the listOfRecipes to set
*/
public void setListOfRecipes(ArrayList<SteppingStone5_Recipe> listOfRecipes) {
this.listOfRecipes = listOfRecipes;
}
public SteppingStone6_RecipeBox() {
this.listOfRecipes = new ArrayList();
}
public SteppingStone6_RecipeBox(ArrayList<SteppingStone5_Recipe> listOfRecipes) {
this.listOfRecipes = listOfRecipes;
}
public void printAllRecipeDetails(String selectedRecipeName) {
for (int i = 0; i < listOfRecipes.size(); i++) {
if (!listOfRecipes.get(i).getRecipeName().equals(selectedRecipeName)) {
} else {
listOfRecipes.get(i).printRecipe();
}
}
}
public void printAllRecipeNames() {
for (SteppingStone5_Recipe currentRecipe : listOfRecipes) {
System.out.println(currentRecipe.getRecipeName());
}
}
public void addNewRecipe() {
//
//Note: the next line only functions once the SteppingStone5_Recipe
//class "main" method has been replaced with:
// public SteppingStone5_Recipe addNewRecipe()
//
//and the method is set to return new Recipe instead of printing it out
//
listOfRecipes.add(new SteppingStone5_Recipe().createNewRecipe());
}
public static void main(String[] args) {
SteppingStone6_RecipeBox myRecipeBox = new SteppingStone6_RecipeBox();
Scanner menuScnr = new Scanner(System.in);
System.out.println("Menu\n" + "1. Add Recipe\n" + "2. Print All Recipe Details\n" + "3. Print All
Recipe Names\n" + "\nPlease select a menu item:");
while (menuScnr.hasNextInt() || menuScnr.hasNextLine()) {
System.out.println("Menu\n" + "1. Add Recipe\n" + "2. Print All Recipe Details\n" + "3. Print
All Recipe Names\n" + "\nPlease select a menu item:");
int input = menuScnr.nextInt();
if (input == 1) {
myRecipeBox.addNewRecipe();
} else if (input == 2) {
System.out.println("Which recipe?\n");
String selectedRecipeName = menuScnr.next();
myRecipeBox.printAllRecipeDetails(selectedRecipeName);
} else if (input == 3) {
for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++) {
System.out.println((j + 1) + ": " +
myRecipeBox.listOfRecipes.getRecipeName().get(j));
}
} else {
System.out.println("\nMenu\n" + "1. Add Recipe\n" + "2. Print Recipe\n" + "3. Adjust
Recipe Servings\n" + "\nPlease select a menu item:");
}
System.out.println("Menu\n" + "1. Add Recipe\n" + "2. Print All Recipe Details\n" + "3. Print
All Recipe Names\n" + "\nPlease select a menu item:");
}
}
}
这是 SteppingStone5_Recipe 类
package SteppingStones;
import java.util.Scanner;
import java.util.ArrayList;
public class SteppingStone5_Recipe {
private String recipeName;
private int servings;
private ArrayList<String> recipeIngredients;
private double totalRecipeCalories;
//accessor/getter for recipe name
public String getRecipeName() {
return recipeName;
}
//mutator/setter for recipe name
public void setRecipeName(String recipeName){
this.recipeName = recipeName;
}
//accessor for servings
public int getServings(){
return servings;
}
//mutator for servings
public void setServings(int servings){
this.servings = servings;
}
//accessor for recipe ingredients
public ArrayList<String> getRecipeIngredients(){
return recipeIngredients;
//mutator for recipe ingredients
}
public void setRecipeIngredients(ArrayList<String> recipeIngredients){
this.recipeIngredients = recipeIngredients;
}
//accessor for the total of recipe calories
public double getTotalRecipeCalories(){
return totalRecipeCalories;
}
//mutator for Total of recipe calories
public void setTotalRecipeCalories(double totalRecipeCalories){
this.totalRecipeCalories = totalRecipeCalories;
}
public SteppingStone5_Recipe() {
this.recipeName = "";
this.servings = 0;
this.recipeIngredients = new ArrayList<>();
this.totalRecipeCalories = 0;
}
public SteppingStone5_Recipe(String recipeName, int servings, ArrayList<String> recipeIngredients,
double totalRecipeCalories) {
this.recipeName = recipeName;
this.servings = servings;
this. recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
}
public void printRecipe() { //method to print all instances of the recipe
double singleServingCalories = totalRecipeCalories / servings;
System.out.println("Recipe: " + getRecipeName());
System.out.println("Serves: " + getServings());
System.out.println("Ingredients:");
for(int i = 0; i < recipeIngredients.size(); ++i) {
System.out.println(recipeIngredients.get(i));
}
System.out.println("Total Calories per serving: " + singleServingCalories);
}
public static void main(String[] args) {
double totalRecipeCalories = 0;
ArrayList<String> recipeIngredients = new ArrayList();
boolean addMoreIngredients = true;
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter the recipe name: ");
String recipeName = scnr.nextLine();
System.out.println("How many servings: ");
int servings = scnr.nextInt();
do {//Loop to add new ingredients until user inputs end
System.out.println("Please enter the ingredient name" +
" or type end if you are finished entering ingredients: ");
String ingredientName = scnr.next();
if (ingredientName.toLowerCase().equals("end")) {
addMoreIngredients = false;
} else {
//Add the ingredient name to recipeIngredients
recipeIngredients.add(ingredientName);
System.out.println("Please enter the ingredient amount: ");
float ingredientAmount = scnr.nextFloat();
System.out.println("Please enter the ingredient Calories: ");
int ingredientCalories = scnr.nextInt();
//Add the total Calories from this ingredient
totalRecipeCalories = ingredientCalories * ingredientAmount;
}
} while (addMoreIngredients) ;
//create new object recipe1 and use method printRecipe to display all properties of recipe
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName,
servings, recipeIngredients, totalRecipeCalories);
recipe1.printRecipe();
}
}
我完全不知道缺少什么或我做错了什么。
解决方案
我无法重现您提到的错误,但是在执行代码时我发现了一些编译时错误,详细信息如下 -
A)在SteppingStone6_RecipeBox
添加新配方方法时addNewRecipe
无法createNewRecipe
在课堂上找到SteppingStone5_Recipe
//TODO Method not found
listOfRecipes.add(new SteppingStone5_Recipe().createNewRecipe());
B)我SteppingStone6_RecipeBox
在您以错误方式获取食谱的主要方法中发现的第二个错误。
for (int j = 0; j < myRecipeBox.listOfRecipes.size(); j++) {
//TODO Changes Done..Kandy
//System.out.println((j + 1) + ": " + myRecipeBox.listOfRecipes.getRecipeName().get(j));
System.out.println((j + 1) + ": " + myRecipeBox.listOfRecipes.get(j).getRecipeName()); // correct code
}
一旦我评论了点 A 并在我运行程序时更正了点 B 运行它。
推荐阅读
- ios - ViewController 中的多个 UICollectionViews | 不会调用一个 CollectionViews 委托方法
- regex - 使用python正则表达式分隔字符串
- java - 创建 GoogleApiClient 时出错
- excel - 动态图表 VBA 的范围为空
- javascript - react-router-dom 给了我 404
- php - 对嵌套数组中的数据进行计数和求和
- kotlin - 如何理解这段 Kotlin 代码?
- docker - 将 Ubuntu Docker 容器作为 Marathon 应用程序运行不断重启
- c# - C#/XAML DataGridView 过滤
- php - 在本地机器上从 Amazon S3 下载文件,而不让用户知道 codeigniter PHP 中的文件来源