一、选择题
1、以下选项中,8大基本数据类型有哪些? (多选) ABCD
A、int B、byte C、Boolean D、short
2、假设int x = 2; x=2+5; x=x+2; x的值是以下哪一个? (D)
A、0 B、2 C、7 D、9
二、程序分析题
- 阅读下面的程序,分析代码是否能够编译通过,如果能编译通过,请列出运行的结果。否则请说明编译失败的原因。
public class Test02 {
public static void main(String[] args){
int x = 12;
{
int y = 96;
System.out.println("x is " + x);
System.out.println("y is " + y);
}
y = x;
System.out.println("x is " + x);
}
}
不能通过。因为会找不到符号无法解析变量‘y’。
public class Test02 {
public static void main(String args[]) {
int n = 9;
System.out.println(n);
char n=’9’;
}
}
答案:不能通过。因为范围中已经定义了变量“n”。
三、编程题目:
1.定义两个整数类型,分别存放相应数字。进行加减乘除运算后输出结果。
- 程序如下:
package com.softTian;
public class Demo2 {
public static void main(String[] args){
int x=6;
int y=3; //定义两个变量x和y并赋值
int z=x+y; //两数相加
System.out.println("z = "+z);
z = x-y; //两数相减
System.out.println("z = "+z);
z = x*y; //两数相乘
System.out.println("z = "+z);
z = x/y; //两数相除
System.out.println("z = "+z);
}
}
结果如图所示:
四、问答题
1.八大基本类型的都有哪些,他们占用字节数多少?
①byte 1字节(8位)、②short 2字节(16位)、③int 4字节(32位)、
④Long 8字节(64位)、⑤float 4字节(32位)、⑥double 8字节(64位)、
⑦Char 2字节(16位)、⑧boolean 1字节(8位)
五、编程题
1、编写代码实现如下内容:if语句、switch实现
考试成绩分等级。
90~100 A等。
80-89 B等。
70-79 C等。
60-69 D等。
60以下 E等。
请根据给定成绩,输出对应的等级。
- If语句如下:
package com.softTian;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //创建Scanner对象,获取用户输入数据
System.out.println("请输入成绩:");
int score = sc.nextInt(); //获取用户输入内容
System.out.println("成绩:" + score);
if (score >= 0 && score <= 100) { //判断程序是否合法(0-100)
if (score >= 90) {
System.out.println("成绩为A");
} else if (score >= 80) {
System.out.println("成绩为B");
} else if (score >= 70) {
System.out.println("成绩为C");
} else if (score >= 60) {
System.out.println("成绩为D");
} else {
System.out.println("成绩为E");
}
}
}
}
结果如图所示:
- switch语句:
package com.softTian;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //创建Scanner对象,获取用户输入数据
System.out.println("请输入成绩:");
int score = sc.nextInt(); //获取用户输入内容
System.out.println("成绩:" + score);
if (score >= 0 && score <= 100) { //判断程序是否合法(0-100)
score /=10; //score=score/10
switch (score){
case 10:
case 9:
System.out.println("成绩为A");
break;
case 8:
System.out.println("成绩为B");
break;
case 7:
System.out.println("成绩为C");
break;
case 6:
System.out.println("成绩为D");
break;
default:
System.out.println("成绩为E");
}
}else{
System.out.println("成绩不合法");}
}
}
结果如图所示:

版权声明:本文为kui576原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。