【发布时间】:2016-02-10 19:49:42
【问题描述】:
好的,所以这可能是一个冗长的,但目前我被困在如何在这种情况下为每个新配方创建一个成分列表,因为当我运行这两个程序时,它会不断地将对象添加到相同的列表。任何有关缩短代码或更好方法的建议都将不胜感激,但为每次迭代制作一种实例变量是重中之重。谢谢。澄清一下,每次运行 AddRecipe 类时,我都需要一个特定于该配方的新成分列表,而不是所有配方中的大量成分列表。
问题;每次我遍历我的 addIngrediant 类时,我都希望使用它的 AddRecipe 类有一个不同的成分值。 (对于每个配方,新成分)我不知道如何做到这一点,并提供了两种方法的示例。
在食谱类中
public static void addIngrediants(String EcipeName){
if(Recipe.recipeName.equals(EcipeName)){
List<List<String>> lists = new ArrayList<List<String>>();
for (int i = 0; i < 100; i++) {
System.out.println("Would you like to add an Ingrediant(YES or NO)");
Scanner yesSir = new Scanner(System.in);
String inputSir = yesSir.nextLine();
if(inputSir.equals("YES")){
System.out.println("Enter your Ingrediant; ");
String userIngred = yesSir.nextLine();
Ingred.add(userIngred);
System.out.println("Your Ingrediant is; " + userIngred);
System.out.println("Your new ingrediant list is now; " + Ingred);
}
else{
System.out.println("Okay thanks!");
break;
}
}
}
}
在包装类中
public static void AddRecipe(){
Recipe jackson = new Recipe();
int index = 0;
for(int i = 0; i<= recipeBook.size();){
System.out.println("============================");
System.out.println("Welcome to the Recipe Book!");
System.out.println("============================");
jackson.getDirections();
jackson.getCalorieCount();
jackson.getHi();
jackson.getRecipeName();
System.out.println("Would you like to add a recipe?(Type YES or NO)");
Scanner wantTo = new Scanner(System.in);
String bad = wantTo.nextLine();
if(bad.equals("YES")){
index++;
System.out.println("Great, now lets name your Recipe;");
String yesName = wantTo.nextLine();
jackson.setRecipeName(yesName);
System.out.println("First we'll start with Ingrediants, Input as many as you like");
Recipe.addIngrediants(yesName);
System.out.println("Moving on, Input your Directions;");
String bud = wantTo.nextLine();
jackson.setDirections(bud);
System.out.println("Now input a Calorie Count (Numbers Only!)");
int jman = wantTo.nextInt();
jackson.setCalorieCount(jman);
提前致谢!
【问题讨论】:
-
我投票关闭是因为:“寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及重现它所需的最短代码问题本身。没有明确问题陈述的问题对其他读者没有用处。"
-
我以为我很清楚这个问题,但如果它不明显,我会编辑它。
标签: java for-loop arraylist java.util.scanner