1.编写代码实现如下内容:if语句实现考试成绩分等级(使用switch语句)。
[90-100] A等。
[80-90) B等。
[70-80) C等。
[60-70) D等。
[0-60) E等。
请根据给定成绩,输出对应的等级。
说明:"[“表示包含,”)"表示不包含
代码:
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
// 键入学生成绩
char c;
while (true) {
System.out.println("请输入学生成绩");
@SuppressWarnings("resource")
int studentScore = new Scanner(System.in).nextInt();
if (studentScore >= 90 && studentScore <= 100) {
c = 'A';
break;
} else if (studentScore >= 80 && studentScore < 90) {
c = 'B';
break;
} else if (studentScore >= 70 && studentScore < 80) {
c = 'C';
break;
} else if (studentScore >= 60 && studentScore < 70) {
c = 'D';
break;
} else if (studentScore >= 0 && studentScore < 60) {
c = 'E';
break;
} else {
System.out.println("数值超出范围,请重新输入!");
}
}
switch (c) {
case 'A':
System.out.println("A");
break;
case 'B':
System.out.println("B");
break;
case 'C':
System.out.println("C");
break;
case 'D':
System.out.println("D");
break;
case 'E':
System.out.println("E");
break;
default:
System.out.println("出现错误!");
}
}
}
2.分析以下需求,并用代码实现:
(1)键盘录入三个整数,按照从小到大的顺序输出
(2)如果用户输入的是3 2 1,程序运行后打印格式"按照从小到大排序后的顺序为:1 2 3"
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
@SuppressWarnings("resource")
String str[] = new Scanner(System.in).nextLine().split(" ");
int a = Integer.valueOf(str[0]), b = Integer.valueOf(str[1]), c = Integer.valueOf(str[2]);
System.out.println(Math.min(Math.min(a, b), c) + " "
+ (a > Math.min(Math.min(a, b), c) && a < Math.max(Math.max(a, b), c) ? a
: b > Math.min(Math.min(a, b), c) && b < Math.max(Math.max(a, b), c) ? b
: c > Math.min(Math.min(a, b), c) && c < Math.max(Math.max(a, b), c) ? c : 0)
+ " " + Math.max(Math.max(a, b), c));
}
}
Tips:
利用三元判断式和Math函数,先输出最小数,
用a和最大数、最小数对比,如果a介于二者之间,输出a
否则用b和最大值、最小值对比,如果b介于二者之间,输出b
否则用c和最大值、最小值对比,如果c介于二者之间,输出c
3.看程序,分析下面程序的结果:
int x = 2,y=3;
switch(x)
{
default:
y++;
case 3:
y++;
break;
case 4:
y++;
}
System.out.println("y="+y);
答:y = 5;*****执行default和case 3,输出为5
4:数组的概念?有什么特点?
数组是储存相同类型数据的容器
长度不可变
元素类型相同
5:一维数组的定义格式?
数组类型 数组名[] = new 数组类型[数组长度];
数组类型 数组名[] = {元素1,元素2,元素3…元素n}; 数组类型
数组名[] = new 数组类型[]{元素1,元素2,元素3…元素n};
6:数组操作的两个小问题?
不能越界
下标从0开始
7.数组常见操作:
数组遍历(依次输出数组中的每一个元素) 一维数组:
class Demo5 {
public static void main(String[] args) {
int arr[] = {1,3,5,7,8,4};
for (int a = 0 ; a < arr.length ; a++){
System.out.print(arr[a]+" ");
}
}
}
8.数组获取最值(获取数组中的最大值最小值)
class Demo5 {
public static void main(String[] args) {
int arr[] = { 2, 4, 6, 7, 8, 9, 5, 7 };
for (int a = 0; a < arr.length; a++) {
for (int b = 0; b < arr.length; b++) {
if (arr[a] < arr[b]) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
}
System.out.println("max = "+arr[arr.length-1]);
}
}
9.数组查表法(根据键盘录入索引,查找对应星期)
public class Demo7 {
public static void main(String[] args) {
String str[] = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
System.out.println(str[new Scanner(System.in).nextInt()-1]);
}
}
10.二维数组遍历获取到每一个值
int[][] intArr = {{12,3,8},{11,8,32,7},{13,44,55}};
public class Demo5 {
public static void main(String[] args) {
int[][] intArr = {{12,3,8},{11,8,32,7},{13,44,55}};
for(int a = 0 ; a < intArr.length;a++){
for (int b = 0 ; b < intArr[a].length;b++){
System.out.print(intArr[a][b]+" ");
}
System.out.println();
}
}
}