Last active
January 27, 2019 02:04
-
-
Save nexeh/cdb8e46fd64238626a267cffc2615bd1 to your computer and use it in GitHub Desktop.
SteppingStone4_Loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package SteppingStones; | |
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
import java.util.Scanner; | |
import java.util.ArrayList; | |
/** | |
* | |
* @author [email protected] | |
*/ | |
public class SteppingStone4_Loops { | |
private String ANSWER_YES = "y"; | |
private String ANSWER_NO = "n"; | |
public static void main(String[] args) { | |
Scanner scnr = new Scanner(System.in); | |
String recipeName = ""; //The name of the recipe | |
ArrayList<String> ingredientList = new ArrayList(); //List of ingredients | |
String newIngredient = ""; //Yes or No for new ingredient | |
boolean addMoreIngredients = true; //Reiterate the loop | |
System.out.println("Please enter the recipe name: "); | |
recipeName = scnr.nextLine(); //Store user input in recipeName | |
do { | |
System.out.println("Would you like to enter an ingredient: (y or n)"); | |
String reply = scnr.next().toLowerCase(); | |
scnr.nextLine(); //Store user input in reply variable | |
//If reply is "y", request ingredient name and add to ingredient list | |
if (reply.equals(ANSWER_YES)) { | |
newIngredient = ANSWER_YES; | |
System.out.println("Enter ingredient name: "); | |
ingredientList.add(scnr.nextLine()); | |
} | |
//Ask if the loop should reiterate | |
System.out.println("Anything else? (y or n)"); | |
newIngredient = scnr.next().toLowerCase(); | |
//If reply is "n", do not reiterate the loop | |
if (newIngredient.equals(ANSWER_NO)) { | |
addMoreIngredients = false; | |
} | |
} while (addMoreIngredients); //Check if addMoreIngredients is true | |
System.out.println(recipeName); | |
//Iterate through each ingredient in the ingredientList array | |
for (int i = 0; i < ingredientList.size(); i++) { | |
String ingredient = ingredientList.get(i); //Get the ingredient for current iteration and store in ingredient string | |
System.out.println(ingredient); //Display ingredient name for this iteration | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment