鱼C论坛

 找回密码
 立即注册
查看: 75|回复: 1

[已解决]怎么写阿

[复制链接]
发表于 2023-11-21 08:31:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题描述】已知公式如下图,利用方法重载编程求f(x,y,z)的值 ,在main方法中实现x,y,z值 的输入及结果的输出 。


                               
登录/注册后可看大图

【输入形式】-1 2 5
【输出形式】0
【样例输入】

【样例输出】
【样例说明】
【评分标准】

最佳答案
2023-11-21 08:31:58
  1. public class FormulaCalculator {
  2.     public int f(int x, int y) {
  3.         return x + y;
  4.     }

  5.     public int f(int x, int y, int z) {
  6.         return x + y - z;
  7.     }

  8.     public static void main(String[] args) {
  9.         FormulaCalculator calculator = new FormulaCalculator();
  10.         int x = Integer.parseInt(args[0]);
  11.         int y = Integer.parseInt(args[1]);
  12.         int z = Integer.parseInt(args[2]);
  13.         int result = calculator.f(x, y, z);
  14.         System.out.println(result);
  15.     }
  16. }
复制代码


这段代码定义了一个名为FormulaCalculator的类,其中包含了两个重载的f方法,分别接受两个和三个参数。在main方法中,通过命令行参数获取x,y,z的值,并调用f方法计算结果,最后输出结果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-21 08:31:58 | 显示全部楼层    本楼为最佳答案   
  1. public class FormulaCalculator {
  2.     public int f(int x, int y) {
  3.         return x + y;
  4.     }

  5.     public int f(int x, int y, int z) {
  6.         return x + y - z;
  7.     }

  8.     public static void main(String[] args) {
  9.         FormulaCalculator calculator = new FormulaCalculator();
  10.         int x = Integer.parseInt(args[0]);
  11.         int y = Integer.parseInt(args[1]);
  12.         int z = Integer.parseInt(args[2]);
  13.         int result = calculator.f(x, y, z);
  14.         System.out.println(result);
  15.     }
  16. }
复制代码


这段代码定义了一个名为FormulaCalculator的类,其中包含了两个重载的f方法,分别接受两个和三个参数。在main方法中,通过命令行参数获取x,y,z的值,并调用f方法计算结果,最后输出结果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2023-12-12 06:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表