简体   繁体   English

已经尝试修复了很多次,我不知道我哪里出错了

[英]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.

相关问题 我不知道BigIntegers出了什么问题 - I don't know where I am going wrong with BigIntegers 我试图在使用后从数组中删除字符串。 但是我不知道我要去哪里错了 - Im trying to remove a string from an array after I use it. But i don't know where I'm going wrong 二次公式错误,我不知道如何解决 - Quadratic formula is wrong and I don't know how to fix 如果我不知道必须执行多少次,如何多次调用子类的方法? - How to call a subclass' method more than once if I don't know how many times I'll have to do that? J2ME上的音频,不知道哪里出错了? - Audio on J2ME, I don't know where is wrong? 如果我不知道数组将有多少个元素? - if i don't know array will be have how many elements? 我知道Java不支持通用数组,但是我不知道如何解决这个问题,因此它可以正常工作 - I know that generic arrays are not supported by Java, but I don't know how to fix this so it will work Java:我无法让它循环,布尔值有问题,但我不知道如何修复它 - Java: I can't get it to loop, somethings wrong with the boolean but I don't know how to fix it 我的手电筒应用程序运行缓慢并且崩溃,我不知道自己在做什么错 - My flashlight app is slow and crashes allot i don't know what I'm doing wrong 我应该得到“无效”作为输出,但我得到“有效”作为输出,不知道为什么输出是错误的 - I should be getting "invalid" as output but I'm getting "valid" as output, don't know why the output is wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM