任务描述
本关任务:将包装类转换成其他数据类型。
相关知识
为了完成本关任务,你需要掌握:1.如何将包装类转换成其他基本数据类型。
将包装类转换成其他数据类型
很简单,我们来看一个例子就知道了:
Integer i = new Integer(100);
//转换成double类型
double d = i.doubleValue();
System.out.println("d的值:" + d);
//转换成float类型
float f = i.floatValue();
System.out.println("f的值" + f);
相信你已经明白怎么进行转换了。
编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End
区域内进行代码补充,具体任务如下:
- 将
int
类型数据转换成其他数据类型。
测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。 预期输出: Integer包装类:67
double类型:67.0
float类型:67.0
int类型:67
package step2;
public class Task {
public static void main(String[] args) {
//请在此添加实现代码
/********** Begin **********/
// 定义int类型变量,值为67
int score = 67;
// 创建Integer包装类对象,表示变量score的值
Integer score1 = new Integer(score);
// 将Integer包装类转换为double类型
double score2 = score1.doubleValue() ;
// 将Integer包装类转换为float类型
float score3 = score1.floatValue() ;
// 将Integer包装类转换为int类型
int score4 = score1.intValue() ;
System.out.println("Integer包装类:" + score1);
System.out.println("double类型:" + score2);
System.out.println("float类型:" + score3);
System.out.println("int类型:" + score4);
/********** End **********/
}
}