import java.util.ArrayList;
import java.util.Scanner;
class SteppingStone4_Loops {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String recipeName = "";
ArrayList<String> ingredientList = new ArrayList();
String newIngredient = "";
boolean addMoreIngredients = true;
System.out.println("Please enter the recipe name: ");
recipeName = scnr.nextLine();
do {
System.out.println("Would you like to enter an ingredient: (y or n)");
String reply = scnr.next().toLowerCase();
/**The code should check the reply:
* "y" --> prompt for the ingredient and add it to the ingredient list;
* "n" --> break out of the loop;
* anything else --> prompt for a "y" or "n"
*/
while (true) {
if (reply.equals("y")) {
System.out.println("Enter ingredient name: ");
newIngredient = scnr.next();
ingredientList.add(newIngredient);
break;
}
else if (reply.equals("n")) {
System.out.println("Goodbye!");
break;
}
else
break;
}
} while (addMoreIngredients);
for (int i = 0; i < ingredientList.size(); i++) {
String ingredient = ingredientList.get(i);
System.out.println(ingredient);
}
}
}
때 프로그램이 반환 실행 :왜이 루프가 처음으로 돌아갑니다?
레시피 이름을 입력하십시오 :
폴렌타를
당신이 성분을 입력 하시겠습니까 (Y 또는 N)
Y
이 성분의 이름을 입력 :
N
당신이 성분을 입력 하시겠습니까 (Y 또는 N) :
소금
당신은 성분을 입력 하시겠습니까 (Y 또는 n)
5
당신이 성분 입력 하시겠습니까 : Y (또는 n)
가 왜 때 응답 = N을 중단하지 않습니다? 왜 "재료를 넣으시겠습니까?" 누군가 내 실수를 지적하거나 다른 방법을 제안 할 수 있습니까? 덕분에
false입니다. 왜 당신은 그들이 다르게 행동 할 것으로 기대합니까? – shmosel
생각해 보니, 당신의 내부 루프는 결코 실제로 반복되지 않습니다. 만약 당신이 그것을 없애 버리면, '끊어 질 것입니다.'가 대신 바깥에서 벗어날 것입니다. – shmosel