英文:
Calculating values with different doubles of various indexes in arrays using methods in Java
问题
我正在创建一个程序,它接收成分及其每盎司卡路里,然后接收配方以及其成分和每种成分的盎司数。我想要计算配方的总卡路里。
import java.util.*;
public class Restaurant {
for (int i=0; i<numRecipes; i++) {
System.out.println(recipeName[i] + ":");
System.out.println("卡路里"); // 在这里调用countCalories方法
}
}
}
英文:
I'm creating a program that takes in ingredients and its calories per ounce, then recipes and its ingredients and ounces of each ingredients. I want to calculate the total calories of the recipes.
import java.util.*;
public class Restaurant {
for (int i=0; i<numRecipes; i++) {
System.out.println(recipeName[i] + ":");
System.out.println(" calories"); // I would call the countCalories method here
}
}
}
答案1
得分: 1
对代码的一些备注:
- 每当您询问一道菜谱的配料时,您都会在 for 循环内部构建一个新的 ingredientsUsed,但您没有将其保留为类级别的变量,因此在每次 for 循环迭代之后它都会被销毁(其作用域在 for 循环内部)。如果最终您只想知道每个菜谱的总卡路里数,您应该在 numIngredients 循环之后计算这些卡路里,但仍然在 numRecipes 循环内部,以便您仍然可以使用 ingredientsUsed 列表。
- 要计算一个菜谱的总卡路里数,您需要计算每个配料的卡路里。calories[0] 中的值对应于 ingredientName[0],但这不一定与 ingredientsUsed[0] 相同。因此,对于每个 ingredientsUsed 元素,您首先需要在 ingredientName 中查找配料,以了解其索引号,然后可以使用该索引号在卡路里数组中查找该配料的卡路里。
- 如果您真的想知道所有菜谱及其使用的配料,您需要构建一个二维数组,其中第一维是每个菜谱,第二维是菜谱的配料。
String[] recipeName = new String[numRecipes];
// 用于保存每个菜谱的卡路里的额外数组
double[] recipeCalories = new double[numRecipes];
for (int i = 0; i < numRecipes; i++) {
recipeName[i] = scan.next();
int numIngredients = scan.nextInt();
String[] ingredientsUsed = new String[numIngredients];
double[] numOunces = new double[numIngredients];
for (int j = 0; j < numIngredients; j++) {
ingredientsUsed[j] = scan.next();
numOunces[j] = scan.nextDouble();
}
recipeCalories[i] = countCalories(ingredientsUsed, ingredientName, calories, numOunces);
}
for (int i = 0; i < numRecipes; i++) {
System.out.println(recipeName[i] + ": calories " + recipeCalories[i]);
}
/**
* ingredientsUsed:菜谱中所有配料的名称
* ingredientName:所有已知配料的名称
* calories:每种 ingredientName 的卡路里
* numOunces:每种 ingredientsUsed 的盎司数
*/
int countCalories(String[] ingredientsUsed, String[] ingredientName, double[] calories, double[] numOunces) {
int cal = 0;
for (int i = 0; i < ingredientsUsed.length; i++) {
for (int n = 0; n < ingredientName.length; n++) {
if (ingredientsUsed[i].equals(ingredientName[n])) {
// n 是 ingredientName 和 calories 数组中对应的索引
cal = cal + ((int) (numOunces[i] * calories[n] + 0.5));
}
}
}
return cal;
}
英文:
A few remarks on the code:
-
Each time you ask for the ingredients of a reciet you build a new ingredientsUsed inside the for loop but you don't keep it as class level so after each for loop iteration it is destroyed (it's scope is within the for loop). If in the end you only want to know the total number of calories per recipe, you should calculate these calories after numIngredients loop, but still within the numRecipes loop so you still have your ingredientsUsed list
-
To calculate the total number of calories for one recipe, you need to calculate the calories of each ingredient. The value in calories[0] corresponds to ingredientName[0] but this is not necessarily the same as ingredientsUsed[0]. So for every ingredientsUsed element you first need to lookup the ingredient in ingredientName to know its index number which you then can use to lookup the calories for that ingredient.
-
If you really want to know all recipes and their ingredientsUsed, you need to construct a 2-dimensional array where dimension one is per recipe and dimension 2 is ingredientUsed for a recipe.
String[] recipeName = new String[numRecipes]; // extra array to keep calories per recipe double[] recipeCalories = new double[]; for (int i=0; i<numRecipes; i++) { recipeName[i] = scan.next(); int numIngredients = scan.nextInt(); String[] ingredientsUsed = new String[numIngredients]; double[] numOunces = new double[]; for (int j=0; j<numIngredients; j++) { ingredientsUsed[j] = scan.next(); numOunces[j] = scan.nextDouble(); } recipeCalories [i]=countCalories(ingredientsUsed,ingredientName,calories,numOunces); } for (int i=0; i<numRecipes; i++) { System.out.println(recipeName[i] + ": calories "+recipeCalories[i]); } /** * ingrediensUsed : names of all ingredients in the recipe * ingredientName: names of all known ingredients * calories: calories per ingredientName * numOunces : ounces per ingredientUsed */ int countCalories (String[] ingredientsUsed, String[] ingredientName, double[] calories, double[] numOunces) { int cal=0; for (int i=0;i<ingredientsUsed.length;i++) { for (int n=0;n<ingredientName.length;n++) { if (ingredientsUsed[i].equals(ingredientName[n]) { // n is the corresponding index in the ingredientName and calories arrays cal = cal+ ((int)(numOunces[i] * calories[n] + 0.5)); } } } return cal; }
答案2
得分: 1
首先,您需要修改countCalories
函数,以便您传入食材的名称。这将允许您查找所使用食材的卡路里:
public static int countCalories(String[] ingredientName, String[] ingredientsUsed, double[] calories, double[] numOunces) {
double cal = 0;
for (int i = 0; i < ingredientsUsed.length; i++) {
for (int j = 0; j < ingredientName.length; j++) {
if (ingredientsUsed[i].equals(ingredientName[j])) {
cal += numOunces[i] * calories[j];
}
}
}
return (int) Math.round(cal);
}
调用这个方法时,我会将两个数组的循环合并成一个循环,并在读取每个食谱时计算卡路里。这样,您就不必一次存储多个食谱的食材和数量。
for (int i = 0; i < numRecipes; i++) {
recipeName[i] = scan.next();
int numIngredients = scan.nextInt();
String[] ingredientsUsed = new String[numIngredients];
double[] numOunces = new double[numIngredients];
for (int j = 0; j < numIngredients; j++) {
ingredientsUsed[j] = scan.next();
numOunces[j] = scan.nextDouble();
}
System.out.println(recipeName[i] + ":");
System.out.println(countCalories(ingredientName, ingredientsUsed, calories, numOunces));
}
英文:
First you need to change countCalories
so that you pass in the names of ingredients. This will let you find the calories for a used ingredient:
public static int countCalories(String[] ingredientName, String[] ingredientsUsed, double[] calories, double[] numOunces) {
double cal = 0;
for (int i = 0; i < ingredientsUsed.length; i++) {
for (int j = 0; j < ingredientName.length; j++) {
if (ingredientsUsed[i].equals(ingredientName[j])) {
cal += numOunces[i] * calories[j];
}
}
}
return (int) Math.round(cal);
}
To call this method, what I would do is merge the two loops over the arrays into one, and count the calories for each recipe as they are read in. This way you don't have to store the ingredients and amounts for more than one recipe at a time.
for (int i = 0; i < numRecipes; i++) {
recipeName[i] = scan.next();
int numIngredients = scan.nextInt();
String[] ingredientsUsed = new String[numIngredients];
double[] numOunces = new double[numIngredients];
for (int j = 0; j < numIngredients; j++) {
ingredientsUsed[j] = scan.next();
numOunces[j] = scan.nextDouble();
}
System.out.println(recipeName[i] + ":");
System.out.println(countCalories(ingredientName, ingredientsUsed, calories, numOunces));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论