double A = Integer.parseInt(args[0])

Integer.parseInt(args[0])中,Integer是java中的一个内置类,parseInt()是这个类的一个静态方法,这个方法的作用是把括号里面的参数(args[0])转为int型的值

但是Integer.parseInt(args[0])要怎么用?

题目
在这里插入图片描述

这样直接运行会报错
要先往命令行输入才能运行

在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package transpotion; import java.lang.reflect.InvocationTargetException; public class ComputeTime { public static void main(String[] args) { System.out.println("Mode of transportation is:"+args[0]); System.out.println("A="+args[1]); System.out.println("B="+args[2]); System.out.println("C="+args[3]); double a=Double.parseDouble(args[1]); double b=Double.parseDouble(args[2]); double c=Double.parseDouble(args[3]); double v,t; try { Common d=(Common) Class.forName("transpotion." +args[0]).getDeclaredConstructor().newInstance(); v=d.speed(a,b,c); t=1000/v; System.out.println("速度=:"+v+"km/h"); System.out.println("时间=:"+(float)t+"h"); } catch(ClassNotFoundException| InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { System.out.println("class not found"); } } } package transpotion; public interface Common { double speed(double a,double b,double c); } package transpotion; public class Car007 implements Common{ public double speed(double a, double b, double c) { if(c==0) { System.out.println("Divisor cannot be 0"); return 0; } else return ab/c; } } package transpotion; public class Plane implements Common { public double speed(double a, double b, double c) { return a+b+c; } } package transpotion; public class Ship implements Common { public double speed(double a,double b,double c) { return ab+a*c; } }可以为我讲解一下这段代码中的String tools = args[0]; int a = Integer.parseInt(args[1]); int b = Integer.parseInt(args[2]); int c = Integer.parseInt(args[3]); Common d = (Common) Class.forName("LAB2." + tools).newInstance(); float time = d.time(a, b, c); System.out.println(time);这部分吗,最好详细点
06-10
好的,这段代码的作用是根据不同的交通工具计算从 A 到 B 的时间。具体来说,它根据命令行参数传递的交通工具类型(`args[0]`)、出发地点 A(`args[1]`)、目的地 B(`args[2]`)和速度或航行时间 C(`args[3]`),计算从 A 到 B 所需的时间,并输出结果。 下面是代码的具体实现: ```java String tools = args[0]; // 获取交通工具类型 int a = Integer.parseInt(args[1]); // 获取出发地点 A int b = Integer.parseInt(args[2]); // 获取目的地 B int c = Integer.parseInt(args[3]); // 获取速度或航行时间 C Common d = (Common) Class.forName("transpotion." + tools).newInstance(); // 通过反射机制创建交通工具对象 double time = d.speed(a, b, c); // 调用交通工具对象的 speed() 方法计算时间 System.out.println(time); // 输出时间 ``` 首先,我们从命令行参数中获取交通工具类型、出发地点 A、目的地 B 和速度或航行时间 C,分别保存在 `tools`、`a`、`b` 和 `c` 变量中。 然后,我们使用反射机制创建交通工具对象。具体来说,我们通过 `Class.forName()` 方法获取交通工具类的 Class 对象,然后调用它的 `newInstance()` 方法来创建一个新的交通工具对象。这里我们使用了强制类型转换将对象转换成 `Common` 接口类型,以便调用 `speed()` 方法。 最后,我们调用交通工具对象的 `speed()` 方法,传递出发地点 A、目的地 B 和速度或航行时间 C 作为参数,计算从 A 到 B 所需的时间,并将结果保存在 `time` 变量中。 需要注意的是,这里的 `Common` 接口定义了一个 `speed()` 方法,用于计算从 A 到 B 所需的时间。具体的实现是在各个交通工具类中完成的。因此,我们可以通过调用 `Common` 接口的 `speed()` 方法,来得到不同交通工具的时间计算结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值