为什么从菜单中选择数组对象的某些功能时不起作用?
Why some of the functions of array objects are not working when selecting them from the menu?
我在 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.");
}
}
预期 Input/Output:
***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.
实际 Input/Output:
***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.
我调试了你的代码,你没有保存“Recipe.number”。在这一行中创建食谱时,您应该添加 recNo 变量:
r = new Recipe(name, recNo, instr, ingrNum, ingred, insNum);
并更改食谱 class,添加:
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;
}
希望对你有所帮助。
已编辑:
我建议您将消息“输入要删除的食谱名称:”更改为“输入要删除的食谱编号:”
我在 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.");
}
}
预期 Input/Output:
***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.
实际 Input/Output:
***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.
我调试了你的代码,你没有保存“Recipe.number”。在这一行中创建食谱时,您应该添加 recNo 变量:
r = new Recipe(name, recNo, instr, ingrNum, ingred, insNum);
并更改食谱 class,添加:
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;
}
希望对你有所帮助。
已编辑: 我建议您将消息“输入要删除的食谱名称:”更改为“输入要删除的食谱编号:”