一维数组的练习

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值