我在 Java 編程中創建了這個程式,以從用戶那里獲取所有食譜詳細資訊輸入,然后用戶可以通過從選單中選擇一個選項來選擇插入、查找、洗掉或顯示食譜詳細資訊。但是,選項 2、3、4 似乎無法正常作業,我一直在嘗試修改和運行但仍然無法正常作業。如何更正代碼以使這些選單選項能夠正常作業。
這是我的代碼
import java.util.Scanner;
class Recipe{
String name;
int number;
String[] instructions;
int numberOfInstructions;
String[] ingredients;
int numberOfIngredients;
public Recipe(String name, String[] instructions, int numberOfInstructions, String[] ingredients, int numberOfIngredients) {
this.name = name;
this.instructions = instructions;
this.numberOfInstructions = numberOfInstructions;
this.ingredients = ingredients;
this.numberOfIngredients = numberOfIngredients;
}
public Recipe() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getInstructions() {
return instructions;
}
public void setInstructions(String[] instructions) {
this.instructions = instructions;
}
public int getNumberOfInstructions() {
return numberOfInstructions;
}
public void setNumberOfInstructions(int numberOfInstructions) {
this.numberOfInstructions = numberOfInstructions;
}
public String[] getIngredients() {
return ingredients;
}
public void setIngredients(String[] ingredients) {
this.ingredients = ingredients;
}
public int getNumberOfIngredients() {
return numberOfIngredients;
}
public void setNumberOfIngredients(int numberOfIngredients) {
this.numberOfIngredients = numberOfIngredients;
}
}
public class Cookbook {
Recipe listOfRecipes[];
int numberOfRecipes;
public Cookbook() {
this(new Recipe [0], 0);
}
public Cookbook(Recipe[] listOfRecipes, int numberOfRecipes) {
this.listOfRecipes = listOfRecipes;
this.numberOfRecipes = numberOfRecipes;
}
public Recipe[] getListOfRecipes() {
return listOfRecipes;
}
public void setListOfRecipes(Recipe[] listOfRecipes) {
this.listOfRecipes = listOfRecipes;
}
public int getNumberOfRecipes() {
return numberOfRecipes;
}
public void setNumberOfRecipes(int numberOfRecipes) {
this.numberOfRecipes = numberOfRecipes;
}
public void addRecipe(Recipe recipe){
Recipe newList[] = new Recipe[this.listOfRecipes.length 1];
for (int i = 0; i < newList.length - 1; i )
{
newList[i] = this.listOfRecipes[i];
}
newList[newList.length - 1] = recipe;
this.listOfRecipes = newList;
}
public Recipe findRecipe(int recipeNo){
for (int i = 0; i < this.listOfRecipes.length; i )
{
if(this.listOfRecipes[i].number == recipeNo)
{
System.out.println(this.listOfRecipes[i]);
return this.listOfRecipes[i];
}
else {
System.out.println("Recipe not found!");
}
}
return null;
}
public void removeRecipe(int recipeNo){
Recipe newList[] = new Recipe[this.listOfRecipes.length];
for (int i = 0; i < newList.length; i )
{
if(this.listOfRecipes[i].number == recipeNo)
{
listOfRecipes[i] = listOfRecipes[this.listOfRecipes.length-1];
listOfRecipes[this.listOfRecipes.length-1] = null;
}
newList[i] = this.listOfRecipes[i];
}
this.listOfRecipes = newList;
System.out.println("Recipe removal successful!");
}
public void display(Recipe recipe){
//Recipe newList[] = new Recipe[this.listOfRecipes.length];
System.out.println("List of recipes: ");
for (int i = 0; i < this.listOfRecipes.length; i )
{
System.out.println(this.listOfRecipes[i].name);
}
}
}
class Testmain{
public static void main(String[] args) {
int i, choose, insNum, ingrNum;
String name, sName, delName;
String[] ingred;
String[] instr;
Scanner ip = new Scanner(System.in);
Cookbook c = new Cookbook();
Recipe r = null;
int n, nn, recNo, recNoo;
do{
System.out.println(" ");
System.out.println("***MENU***");
System.out.println("1. Add Recipe");
System.out.println("2. Find Recipe");
System.out.println("3. Remove Recipe");
System.out.println("4. Display Recipe");
System.out.print("Enter your choice: ");
choose = ip.nextInt();
System.out.println();
if(choose == 1) {
System.out.print("How many recipes would you like to add?: ");
n = ip.nextInt();
for (int j=0; j<n; j ){
System.out.print("\nEnter number of recipe: ");
recNo = ip.nextInt();
System.out.print("Enter recipe name: ");
ip.nextLine();
name = ip.nextLine();
System.out.print("Enter number of ingredients: ");
ingrNum = ip.nextInt();
ingred = new String[ingrNum];
for(i=0; i<ingrNum; i ) {
System.out.print("Ingredient " (i 1) ": ");
ingred[i] = ip.next();
}
System.out.print("Enter number of instructions: ");
insNum = ip.nextInt();
instr = new String[insNum];
for(i=0; i<insNum; i ) {
System.out.print("Instruction " (i 1) ": ");
ip.nextLine();
instr[i] = ip.next();
}
r = new Recipe(name, instr, ingrNum, ingred,insNum);
c.addRecipe(r);
}
} else if(choose == 2) {
System.out.println("Enter number of recipe you want to find: ");
recNo = ip.nextInt();
c.findRecipe(recNo);
} else if(choose == 3) {
System.out.println("Enter name of recipe to be removed: ");
ip.nextLine();
recNoo = ip.nextInt();
c.removeRecipe(recNoo);
}
else if(choose == 4) {
c.display(r);
}
else {
System.out.println("Invalid entry!");
}
System.out.println(" ");
System.out.println("Would you like to continue?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.print("Your choice: ");
nn = ip.nextInt();
} while (nn != 2);
System.out.println("Program has ended.");
}
}
預期輸入/輸出:
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 1
How many recipes would you like to add?: 2
Enter number of recipe: 1
Enter recipe name: r
Enter number of ingredients: 1
Ingredient 1: i1
Enter number of instructions: 2
Instruction 1: n1
Instruction 2: n2
Enter number of recipe: 2
Enter recipe name: r2
Enter number of ingredients: 1
Ingredient 1: i1
Enter number of instructions: 1
Instruction 1: n1
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 2
Enter number of recipe you want to find:
1
r
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 4
List of recipes:
r
r2
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 3
Enter name of recipe to be removed:
1
Recipe removed successfully!
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 4
List of recipes:
r2
Would you like to continue?
1. Yes
2. No
Your choice: 2
Program has ended.
實際輸入/輸出:
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 1
How many recipes would you like to add?: 2
Enter number of recipe: 1
Enter recipe name: r
Enter number of ingredients: 1
Ingredient 1: i1
Enter number of instructions: 2
Instruction 1: n1
Instruction 2: n2
Enter number of recipe: 2
Enter recipe name: r2
Enter number of ingredients: 1
Ingredient 1: i1
Enter number of instructions: 1
Instruction 1: n1
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 2
Enter number of recipe you want to find:
1
Recipe not found!
Recipe not found!
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 4
List of recipes:
r
r2
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 3
Enter name of recipe to be removed:
1
Recipe removed successfully!
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 4
List of recipes:
r
r2
Would you like to continue?
1. Yes
2. No
Your choice: 2
Program has ended.
uj5u.com熱心網友回復:
我已經除錯了您的代碼,但您沒有保存“Recipe.number”。在此行中創建配方時應添加 recNo 變數:
r = new Recipe(name, recNo, instr, ingrNum, ingred, insNum);
并更改 Recipe 類,添加:
public Recipe(String name, int number, String[] instructions, int numberOfInstructions, String[] ingredients, int numberOfIngredients) {
this.name = name;
this.number = number;
this.instructions = instructions;
this.numberOfInstructions = numberOfInstructions;
this.ingredients = ingredients;
this.numberOfIngredients = numberOfIngredients;
}
我希望這對你有幫助。
已編輯:我建議您將訊息“輸入要洗掉的配方名稱:”更改為“輸入要洗掉的配方編號:”
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465537.html
