选择题
1.C
2.C
3.BC
4.D
5.C
6.B
7.D
8.A
编程题
- 完成基本数据类型和包装类之间的转换。
public class HelloWorld {
public static void main(String[] args) {
// 定义int类型变量,值为100
int score = 100;
// 创建Integer包装类对象,表示变量score1的值
Integer score1 = new Integer(score);
// 将Integer包装类转换为double类型
double d = (double)score1;
// 将Integer包装类转换为long类型
long l = (long)score1;
// 将Integer包装类转换为int类型
int i = (int)score1;
//打印输出
System.out.println("score对应的Integer类型结果为:" + score1);
System.out.println("score对应的double类型结果为:" + d);
System.out.println("score对应的long类型结果为:" + l);
System.out.println("重新由Integer转换为int类型的结果为:" + i);
}
}
- 完成基本数据类型和包装类之间的转换。
public class StringUse {
public static void main(String[] args) {
double a = 12.5;
//将基本类型转换为字符串
String str1 = Double.toString(a);
System.out.println("a 转换为String型后+10的结果为: "+ str1);
String str = "2.8";
// 将字符串转换为基本类型
double b = Double.parseDouble(str);
System.out.println("str 转换为double型后+10的结果为: "+ b);
}
}
- 完成基本数据类型和字符串之间的转换。
public static void main(String[] args) {
//定义float类型变量,赋值为88.99
float f1 = 88.99f;
//将基本类型转换为字符串
String str1 = Float.toString(f1);
//打印输出
System.out.println("f1转换为String型后与整数20的求和结果为:" + str1 + 20);
//定义String类型变量,赋值为"188.55"
String str = "188.55";
// 将字符串转换为基本类型double
double d = Double.parseDouble(str);
//打印输出
System.out.println("str转换为double型后与整数20的求和结果为:" + d + 20);
}