- 阅读权限
- 255
- 威望
- 1 级
- 论坛币
- 49635 个
- 通用积分
- 55.7537
- 学术水平
- 370 点
- 热心指数
- 273 点
- 信用等级
- 335 点
- 经验
- 57805 点
- 帖子
- 4005
- 精华
- 21
- 在线时间
- 582 小时
- 注册时间
- 2005-5-8
- 最后登录
- 2023-11-26
|
Listing 4.16. Recipes Application
- Listing 4.16. Recipes Application
- // Recipes.java
-
- public class Recipes
- {
- public static int calcIngredient( int[] serv, int[][] flour, int column )
- {
- int total = 0, i, j;
-
- for( i = 0, j = 0; i < serv.length; i++, j++)
- total = total + serv[i] * flour[j][column]; // only one column in flour required
-
- return total;
- }
-
- public static void main(String[] args)
- {
- int[ ][ ] recipe = { {2,2,1,0}, {3,0,1,0}, {2,1,2,1}};
- int[ ] serving = {1, 4, 3};
-
- // call to calculate flour
- int cups = calcIngredient(serving, recipe, 0); // only oth column required
- System.out.println(“No. of cups of flour required: ” + cups);
-
- // call to calculate eggs
- int eggs = calcIngredient(serving, recipe, 1); // only 1st column required
- System.out.println(“No. of eggs required: ” + eggs);
-
- // call to calculate milk
- int milk = calcIngredient(serving, recipe, 2); // only 2nd column required
- System.out.println(“No. of cups of milk required: ” + milk);
-
- // call to calculate oil
- int oil = calcIngredient(serving, recipe, 3); // only 3rd column required
- System.out.println(“No. of tablespoons of oil required: ” + oil);
- }
- }
复制代码
|
|