[英]Have tried to fix this so many times I don't know where I'm going wrong
I am very new to java and am trying to learn how to program it while working remotely which is very difficult for me.我对 java 非常陌生,并且正在尝试学习如何在远程工作时对其进行编程,这对我来说非常困难。 The assignment is to create an "ingredient" class that will ask for ingredients, validate the input, and then at the end print a list of ingredients.
任务是创建一个“成分”class,它将询问成分,验证输入,然后在最后打印成分列表。 I have written and re-written this code about a dozen times and it never prints out correctly.
我已经编写并重写了大约十几次此代码,但它从未正确打印出来。 It takes one too many loops through to get a correct output, and when the values stored in the variables during the execution print out incorrectly.
需要多次循环才能获得正确的 output,并且在执行期间存储在变量中的值打印不正确时。 This group is my last ditch effort as I am struggling so hard even though I'm putting in the work.
这个小组是我最后的努力,因为即使我正在努力工作,我也很努力。 Thanks again and take care.
再次感谢并保重。
package Stepping_Stones_Lab_2;
import java.util.Scanner;
import java.util.ArrayList;
public class MilestoneOne {
/**
*
*/
public static void main(String[] args) {
ArrayList<String> ingredientList = new ArrayList();
ArrayList<String> recipeList = new ArrayList();
//variable declaration
double ingredientAmount = 0.0;
double totalCalories = 0.0;
int numberCaloriesPerUnit = 0;
String recipeName;
String nameOfIngredient;
String unitMeasurement;
Scanner scnr = new Scanner(System.in);
boolean addMoreIngredients = true;
//recipie creation
System.out.print("Please enter your new recipe name: ");
recipeName = scnr.nextLine();
recipeList.add(recipeName);
//This do-while loop is requesting ingredients until such time the user terminates the requests by typing n. If neither y or n is
//used the program will return an Invalid value error
do {
System.out.print("Would you like to enter an ingredient: (y or n): ");
String reply = scnr.next().toLowerCase();
scnr.nextLine();
//the notes says use a switch but I have not been able to get a switch to work successfully despite many tries.
if(reply.equals("y")) {
System.out.print("Enter ingredient name: ");
nameOfIngredient = scnr.nextLine();
while (!nameOfIngredient.matches("[a-zA-Z_]+")){ //Validates input and loops until there is acceptable input for name
System.out.print("Error: Please enter valid name of ingredient: ");
nameOfIngredient = scnr.next();
}
ingredientList.add(nameOfIngredient);
System.out.println("Good job! The ingredient you entered is " + nameOfIngredient);
//Unit of measurement input and data validation
System.out.print("Please enter the unit of measurement (cups, ounces, etc.): ");
unitMeasurement = scnr.next();
while (!unitMeasurement.matches("[a-zA-Z_]+")){ //Validates input and loops until there is acceptable input for unit measurement
System.out.print("Error: Please enter valid unit of measurement: ");
unitMeasurement = scnr.next();
}
System.out.println("Good job! The unit of measurement for " + nameOfIngredient + " is " + unitMeasurement);
//Amount of ingredient input and data validation
System.out.print("Please enter the number of " + unitMeasurement + " of " + nameOfIngredient + " we'll need: ");
while (!scnr.hasNextDouble()){//Validates input and loops until there is acceptable input for amount
System.out.print("Error: Please enter the number of " + unitMeasurement + " of " + nameOfIngredient + " we'll need using numbers only: ");
scnr.next();
}
ingredientAmount = scnr.nextDouble();
System.out.println("Good job! The number of " + unitMeasurement + " of " + nameOfIngredient + " needs is " + ingredientAmount);
//Number of calories per cup of ingredient input and data validation
System.out.print("Please enter the number of calories per " + unitMeasurement + " of " + nameOfIngredient + ": ");
while (!scnr.hasNextInt()){
System.out.print("Please enter the number of calories per " + unitMeasurement + " of " + nameOfIngredient + " using numbers only: ");
scnr.next();
}
numberCaloriesPerUnit = scnr.nextInt();
System.out.println("Good job! The number of calories per " + unitMeasurement + " of " + nameOfIngredient + " is " + numberCaloriesPerUnit);
totalCalories = ingredientAmount * numberCaloriesPerUnit;
System.out.println(recipeName + " uses " + ingredientAmount + " " + unitMeasurement + " and has " + totalCalories + " calories.");
}
else if(reply.equals("n")){
addMoreIngredients = false;
System.out.println("Your ingredients for " + recipeName + " are as follows: ");
}
else {
System.out.println("Invalid value!!");
}
}
while (addMoreIngredients);
for (int i = 0; i < ingredientList.size(); i++) {
String ingredient = ingredientList.get(i);
System.out.println(ingredient);
}
}
}
It works for me.这个对我有用。 Look at the following transcript.
看看下面的成绩单。
Please enter your new recipe name: recipe name
Would you like to enter an ingredient: (y or n): y
Enter ingredient name: a
Good job! The ingredient you entered is a
Please enter the unit of measurement (cups, ounces, etc.): b
Good job! The unit of measurement for a is b
Please enter the number of b of a we'll need: 2
Good job! The number of b of a needs is 2.0
Please enter the number of calories per b of a: 3
Good job! The number of calories per b of a is 3
recipe name uses 2.0 b and has 6.0 calories.
Would you like to enter an ingredient: (y or n): y
Enter ingredient name: a2
Error: Please enter valid name of ingredient: aa
Good job! The ingredient you entered is aa
Please enter the unit of measurement (cups, ounces, etc.): bb
Good job! The unit of measurement for aa is bb
Please enter the number of bb of aa we'll need: 4
Good job! The number of bb of aa needs is 4.0
Please enter the number of calories per bb of aa: 8
Good job! The number of calories per bb of aa is 8
recipe name uses 4.0 bb and has 32.0 calories.
Would you like to enter an ingredient: (y or n): n
Your ingredients for recipe name are as follows:
a
aa
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.