Java基础问题(3)——switch循环、数组

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();
		}
	}
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值