【问题标题】:Have tried to fix this so many times I don't know where I'm going wrong试过很多次了,不知道哪里出错了
【发布时间】:2020-07-15 07:24:53
【问题描述】:

我对 java 很陌生,我正在尝试学习如何在远程工作时对其进行编程,这对我来说非常困难。任务是创建一个“成分”类,该类将询问成分,验证输入,然后在最后打印成分列表。我已经编写并重写了十几次此代码,但它从未正确打印出来。需要多次循环才能获得正确的输出,并且在执行期间存储在变量中的值打印不正确时。这个小组是我最后的努力,因为即使我正在努力工作,我也很努力!再次感谢并保重。

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);
}
}
}

【问题讨论】:

  • 定义实际和预期输出。什么不工作?输入是什么?
  • 您能否详细说明问题所在?我运行了你的代码,它似乎可以工作,但我想我不确定最终结果应该是什么。
  • 当我运行代码时,它应该一次请求一个成分。如果用户输入“n”,则应该终止循环并输出在配方中输入的成分列表。当我输入 n 时,它会循环回询问下一个成分。此外,当我更改测量类型(从杯子到盎司)时,它不会打印盎司,而是保持杯子......
  • 我很抱歉@Amongalen 我在提问时应该更准确!从现在开始我会更具体。

标签: java loops methods output recipe


【解决方案1】:

它对我有用。请看以下成绩单。

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

【讨论】:

  • 非常感谢。我终于发现我的编译器一定出了问题。非常感谢您的所有时间。
猜你喜欢
  • 2012-05-09
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多