[英]Have tried to fix this so many times I don't know where I'm going wrong
我對 java 非常陌生,並且正在嘗試學習如何在遠程工作時對其進行編程,這對我來說非常困難。 任務是創建一個“成分”class,它將詢問成分,驗證輸入,然后在最后打印成分列表。 我已經編寫並重寫了大約十幾次此代碼,但它從未正確打印出來。 需要多次循環才能獲得正確的 output,並且在執行期間存儲在變量中的值打印不正確時。 這個小組是我最后的努力,因為即使我正在努力工作,我也很努力。 再次感謝並保重。
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);
}
}
}
這個對我有用。 看看下面的成績單。
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.