array相关练习

42 阅读3分钟

练习有感:

1、String类型的数组初始化时,字符串必须用“”括起。

例题:从键盘输入1-12,显示对应月份的的单词。

import java.util.Scanner;

class Months{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        String [] months ={"January","February","March","April","May","June","July","August","September","October","November","December"};

        System.out.println("Please input the month:");

        int month = input.nextInt();

        if(month<1||month>12){

            System.out.println("the month you input is error.");

        }

        else{

            System.out.println("the month is "+months[month-1]);

        }

        input.close();

    }

    }

 

 

2、题目类似于“将不及格的同学名字打出”,可按如下格式:

 System.out.println("the students who get the best score has:");

            for(int i=0;i<count;i++){

                if(scores[i]==max){

                System.out.println(name[i]);

                }

                }

 

3、题目要求为“随机产生偶数时”,可以运用(要求范围/2)*2的公式,则得到范围内随机偶数。

int even =(int)(Math.random()*50+1)*2;

此时,even为(150)*2=1100范围内的偶数。

例题:随机产生10个[1,100]之间的偶数,存储到数组中,并按照从小到大排序输出。

class RandomEven{

    public static void main(String[] args){

        int []arr=new int [10];

        for(int i=0;i<arr.length;i++){

            int even =(int)(Math.random()*50+1)*2;

                arr[i]=even;

             

        }

        for(int i=0;i<arr.length;i++){

            for(int j=0;j<arr.length-1-i;j++){

                if(arr[j]>arr[j+1]){

                    int t=arr[j];

                    arr[j]=arr[j+1];

                    arr[j+1]=t;

                }

            }

        }

        for(int i=0;i<arr.length;i++){

            System.out.println(arr[i]);

        }

 

    }

}

 

4.输入本组学员人数,再依次输入本组学员的成绩和姓名,最后统计:平均分、低于平均分人数、低于平均分学员姓名、最高分和最低分的姓名

可以理解姓名与成绩为一一对应的两个数组,由此遍历,得出答案。

最高分和最低分可以运用min和max在的常用思想,简单的遍历大小比较可以尝试使用三目运算符。

import java.util.Scanner;

class ExtraExerArray2{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        System.out.println("please input the count of the group:");

        int count = input.nextInt();

       

 

        String [] name = new String [count];

        int [] scores = new int [count];

        for(int i=0;i<count;i++){

            System.out.println("Please input the names of the students:");

            name[i]=input.next();

            System.out.println("Please input the score of "+name[i]);

            scores[i]=input.nextInt();}

        System.out.println("The total students' scores are following:");

        for(int i=0;i<count;i++){

            System.out.println(name[i]+":"+scores[i]);

        }

            int sum=0,amount=0,average=0;

            int max=scores[0],min=scores[0];

            for(int i=0;i<count;i++){

            sum+=scores[i];

            average=sum/count;

            max=scores[i]>max?scores[i]:max;

            min=scores[i]<min?scores[i]:min;

        }

            System.out.println("The average of the scores is "+average);

            System.out.println("the greatest score is "+max);

            System.out.println("the lowest score is "+min);

                System.out.println("the students who get the best score has:");

            for(int i=0;i<count;i++){

                if(scores[i]==max){

                System.out.println(name[i]);

                }

            }

                System.out.println("the students who get the lowest score has:");

                for(int i=0;i<count;i++){

                    if(scores[i]==min){

                    System.out.println(name[i]);

                    }

                }

         

            System.out.println("the students whose score is lower the average has:");

            for(int i=0;i<count;i++){

                if(scores[i]<average){

                    System.out.println(name[i]+",");

                    amount++;

                }

            }

            System.out.println("the amount of the students who didn't pass is "+amount);

        input.close();

    }

}

 

5、输入本组学员的人数,再依次输入本组学员的姓名和成绩,显示学员的姓名和成绩,最后查找是否存在满分学员。若存在,显示学员姓名,否则输出没有满分学员。

 

查找是否有满分学员的题目思路同上。关于查找“是否有...类型存在”,可以运用boolean类型的flag确定是否存在,此种思想常常用于确定某个判断是否存在。

import java.util.Scanner;

class FullScores{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        System.out.println("please input the count:");

        int count = input.nextInt();

        String []name = new String[count];

        int [] scores=new int[count];

        boolean flag = true;

        for(int i=0;i<count;i++){

            System.out.println("please input the name of the student:");

            name[i]=input.next();

            System.out.println("please input the scores of the student:");

            scores[i]=input.nextInt();

        }

        for(int i=0;i<count;i++){

            System.out.println("name:"+name[i]+"score:"+scores[i]);

        }

        for(int i=0;i<scores.length;i++){

            if(scores[i]==100){

                System.out.println("the student who got the full score is "+name[i]);

                flag=false;

            }

            }

        while(flag){

             System.out.println("It's a pity that there is not any student gei full scores.");

        }

       

        input.close();

    }

}