1.从数组中找联系方式
package day06;
public class ArrayDemo {
public static void main(String[] args) {
int[] arr=new int[]{8,4,1,9,3,6,5};
int[] index=new int[]{2,0,4,4,0,1,5,3,3,6,5};
String tel="";
for (int i = 0; i < index.length; i++) {
tel +=arr[index[i]];
}
System.out.println("联系方式:"+tel);
}
}
2.从键盘读入学生成绩,找出最高分,并输出学生成绩等级。
成绩>=最高分-10 等级为‘A’
成绩>=最高分-20 等级为‘B’
成绩>=最高分-30 等级为‘C’
其他 等级为‘D’
提示:先读入学生人数,根据人数创建int数组,存放学生成绩
package day06;
import java.util.Scanner;
public class ArrayDemo {
public static void main(String[] args) {
//1.使用scanner,读取学生个数
Scanner scanner=new Scanner(System.in);
System.out.println("请输入学生人数:");
int number=scanner.nextInt();
//2.创建数组,存储学生成绩;动态初始化
int[] scores=new int[number];
//3.给数组中的元素赋值
System.out.println("请输入"+number+"个学生成绩");
int maxScore=0;
for (int i = 0; i < scores.length; i++) {
scores[i]=scanner.nextInt();
//4.获取数组中的元素最大值:最高分
if (maxScore<scores[i]){
maxScore=scores[i];
}
}
//5.根据每个学生成绩与最高分的差值,得到每个学生的等级,并输出等级和成绩
char level;
for (int i = 0; i < scores.length; i++) {
if (maxScore-scores[i]<=10){
level='A';
}else if (maxScore-scores[i]<=20){
level='B';
}else if (maxScore-scores[i]<=30){
level='C';
}else {
level='D';
}
System.out.println("student "+i+"score is "+scores[i]+",gread is "+level);
}
}
}
运行结果:
请输入学生人数:
5
请输入5个学生成绩
99
85
56
67
77
student 0score is 99,gread is A
student 1score is 85,gread is B
student 2score is 56,gread is D
student 3score is 67,gread is D
student 4score is 77,gread is C
Process finished with exit code 0