好吧,所以這可能是一個漫長的,但目前我堅持如何在這種情況下爲每個新配方創建Ingrediants列表,因爲當我運行這兩個程序截至目前它不斷地添加對象到相同的名單。任何建議縮短代碼或更好的方法將不勝感激,但爲每個迭代做一個實例變量是最重要的。謝謝。爲了澄清,我每次運行AddRecipe類時都需要一個特定於該配方的新成分的新列表,而不是所有配方中的大量Ingrediants。如何在每次for循環命中時創建一個對象列表?
問題;每次我遍歷我的addIngrediant類時,我都希望爲使用它的AddRecipe類創建一個獨特的元素值。 (對於每一個配方,新的Ingrediants)我不知道如何做到這一點,並提供了兩種方法的樣本。
在配方類
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);
謝謝!
我投票關閉是因爲:「尋求調試的問題幫助(」爲什麼不是這個代碼工作?「)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。「 – bhspencer
我以爲我對問題非常清楚,但如果它不是不可知的,我會編輯它。 – BobRoss567