簡體   English   中英

使用Java中的方法計算數組中各種索引的不同雙精度值

[英]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(" calories"); // I would call the countCalories method here
        }
    }

}

關於代碼的幾點說明:

  1. 每次您詢問接收的成分時,您都會在 for 循環內構建一個新的成分Used,但您不將其保留為類級別,因此在每次 for 循環迭代后它都會被銷毀(它的范圍在 for 循環內)。 如果最后你只想知道每個食譜的卡路里總數,你應該在 numIngredients 循環之后計算這些卡路里,但仍然在 numRecipes 循環中,所以你仍然有你的成分使用列表

  2. 要計算一個食譜的卡路里總數,您需要計算每種成分的卡路里。 卡路里[0] 中的值對應於成分名稱[0],但這不一定與成分使用[0] 相同。 因此,對於每個成分使用的元素,您首先需要在成分名稱中查找成分以了解其索引號,然后您可以使用該索引號來查找該成分的卡路里。

  3. 如果你真的想知道所有的食譜和他們使用的成分,你需要構建一個二維數組,其中第一個維度是每個配方,維度 2 是用於一個配方的成分。

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

首先,您需要更改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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM