用Java 函数的参数和返回值。(自定义一个计算器,输入几个头,几个爪,几碗面,单价自己定,并且计算总价)(使用四种方式来完成)

时间: 2023-12-07 13:06:06 浏览: 33
1. 使用基本数据类型实现: ```java public class Calculator { public static void main(String[] args) { int head = 2; int claw = 4; int bowl = 3; double price = 12.5; double total = calculateTotal(head, claw, bowl, price); System.out.println("总价为:" + total); } public static double calculateTotal(int head, int claw, int bowl, double price) { return (head * 2 + claw * 3 + bowl * 1.5) * price; } } ``` 2. 使用数组实现: ```java public class Calculator { public static void main(String[] args) { int[] nums = new int[]{2, 4, 3}; double price = 12.5; double total = calculateTotal(nums, price); System.out.println("总价为:" + total); } public static double calculateTotal(int[] nums, double price) { return (nums[0] * 2 + nums[1] * 3 + nums[2] * 1.5) * price; } } ``` 3. 使用对象实现: ```java public class Calculator { public static void main(String[] args) { Goods goods = new Goods(2, 4, 3, 12.5); double total = calculateTotal(goods); System.out.println("总价为:" + total); } public static double calculateTotal(Goods goods) { return (goods.getHead() * 2 + goods.getClaw() * 3 + goods.getBowl() * 1.5) * goods.getPrice(); } } class Goods { private int head; private int claw; private int bowl; private double price; public Goods(int head, int claw, int bowl, double price) { this.head = head; this.claw = claw; this.bowl = bowl; this.price = price; } public int getHead() { return head; } public void setHead(int head) { this.head = head; } public int getClaw() { return claw; } public void setClaw(int claw) { this.claw = claw; } public int getBowl() { return bowl; } public void setBowl(int bowl) { this.bowl = bowl; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } ``` 4. 使用Map实现: ```java import java.util.HashMap; import java.util.Map; public class Calculator { public static void main(String[] args) { Map<String, Integer> goods = new HashMap<>(); goods.put("head", 2); goods.put("claw", 4); goods.put("bowl", 3); double price = 12.5; double total = calculateTotal(goods, price); System.out.println("总价为:" + total); } public static double calculateTotal(Map<String, Integer> goods, double price) { int head = goods.get("head"); int claw = goods.get("claw"); int bowl = goods.get("bowl"); return (head * 2 + claw * 3 + bowl * 1.5) * price; } } ```

相关推荐

最新推荐

recommend-type

Python实现调用另一个路径下py文件中的函数方法总结

主要介绍了Python实现调用另一个路径下py文件中的函数方法,结合实例形式总结分析了Python针对不同文件夹中py文件调用操作的处理技巧与相关注意事项,需要的朋友可以参考下
recommend-type

Java自定义函数调用方法解析

主要介绍了java自定义函数调用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

C语言中数组作为函数的参数以及返回值的使用简单入门

主要介绍了C语言中数组作为函数的参数以及返回值的使用简单入门,这里以一维数组作为基本条件进行例子讲解,需要的朋友可以参考下
recommend-type

解决使用python print打印函数返回值多一个None的问题

主要介绍了解决使用python print打印函数返回值多一个None的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

C++通过自定义函数找出一个整数数组中第二大数的方法

主要介绍了C++通过自定义函数找出一个整数数组中第二大数的方法,涉及C++针对数组的遍历操作相关技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

从键盘输入一段英文字符串,其中包含多个字母‘h',请编写程序利用正则表达式,将英文字符串中的’h'全部改为‘H’

好的,我可以帮您解决这个问题。 您可以使用 Python 编写一个程序来实现这个功能。具体步骤如下: 1. 首先导入 re 模块,这个模块是 Python 中用于处理正则表达式的模块。 2. 然后定义一个包含英文字符串的变量,例如: text = "This is a string containing multiple h's." 3. 接下来,使用 re.sub() 方法来进行替换操作。re.sub() 方法的第一个参数是正则表达式,第二个参数是替换的字符或字符串,第三个参数是被替换的字符串。在这个例子里,我们需要将所有的小写字母 h 替换成大写字母 H,所以正则表达式可以写成
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。