前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JAVA数组练习

JAVA数组练习

作者头像
拾点阳光
发布2018-05-10 18:11:34
5690
发布2018-05-10 18:11:34
举报
文章被收录于专栏:码云1024码云1024
代码语言:javascript
复制

  1 package com.zhang.hello;
  2 
  3 public class TestArray {
  4     public static int NO1(double [] score){
  5         int count=0;
  6         for(double d:score){
  7             if(d<60){
  8                 count++;
  9             }
 10         }
 11         return count;
 12     }
 13     public static int[] NO2(int [] num){
 14         int [] newnum=new int[num.length];
 15         for(int i=newnum.length-1;i>=0;i--){
 16             newnum[i]=num[num.length-1-i];
 17         }
 18         return newnum;
 19     }
 20     public static int[] NO3(int [] num){
 21         int [] count=new int[10]; 
 22         for(int i:num){
 23             count[i]++;      
 24         }
 25         return count;
 26     }
 27     public static String[] NO4(String [] students,String deleteStuName){
 28         for(int i=0;i<students.length;i++){
 29             if(students[i].equals(deleteStuName)){
 30                 for(int j=i;j<students.length-1;j++){
 31                     students[j]=students[j+1];
 32                 }
 33                 students[students.length-1]=null;
 34                 return students;
 35             }
 36         }
 37         System.out.println(deleteStuName+"不存在");
 38         return students;
 39     }
 40     public static void NO5(){
 41         String str="113@ ere qqq yyui";
 42         String[] str2=str.split("@ | ");
 43         for(String s:str2) System.out.print(s+" ");
 44     }
 45     public static void NO6(){
 46         String str="01#张三#20*02#李四#30*03#王五#40";
 47         String[] str2=str.split("#|\\*");
 48         String[][] str3=new String[3][3];
 49         for(int i=0;i<3;i++){
 50             for(int j=0;j<3;j++){
 51                 str3[i][j]=str2[i*3+j];
 52             }
 53         }
 54         for(int i=0;i<3;i++){
 55             for(int j=0;j<3;j++){
 56                 System.out.print(str3[i][j]+" ");
 57             }
 58             System.out.println();
 59         }
 60         int score=Integer.valueOf(str3[0][2])+Integer.valueOf(str3[1][2])+Integer.valueOf(str3[2][2]);
 61         System.out.println("总成绩:"+score);    
 62     }
 63     public static int[] NO7(int[] nums){
 64         int zeroCount=0;                //统计0的个数
 65         for(int i:nums) if(i==0) zeroCount++;
 66         int [] newArr=new int[nums.length-zeroCount];
 67         int Count=0;                    //计数器
 68         for(int i:nums){
 69             if(i!=0){
 70                 newArr[Count]=i;
 71                 Count++;
 72             }
 73         } 
 74         return newArr;
 75     }
 76     public static void main(String[] args) {
 77         
 78         System.out.println("\n第一题:");
 79         System.out.println("不及格的有:"+NO1(new double[]{70,76,80.5,60,50,61.5,59,90,80,88})+"个");
 80         
 81         
 82         System.out.println("\n第二题:");
 83         for(int i:NO2(new int[]{1,2,3,4,5,6})) System.out.print(" "+i);
 84         System.out.println();
 85         
 86         
 87         System.out.println("\n第三题:");
 88         int [] num=new int[30];
 89         for(int i=0;i<num.length;i++){
 90             num[i]=(int)Math.floor((Math.random()*10));
 91         }
 92         for(int i:num) System.out.print(i+" ");
 93         System.out.println();
 94         int[] count=NO3(num);
 95         for(int i=0;i<count.length;i++){
 96             System.out.println(i+" 出现的次数为: "+count[i]);
 97         }
 98         
 99         
100         System.out.println("\n第四题:");
101         String[] students=new String[]{"小明","老王","张三","赵四","小美","小娜","小华"};
102         System.out.print("删除前的学生:");
103         for(String i:students) System.out.print("\t"+i);
104         students=NO4(students,"张三");
105         System.out.print("\n删除后的学生:");
106         for(String i:students) System.out.print("\t"+i);
107         System.out.println();
108         
109         
110         System.out.println("\n第五题:");
111         NO5();
112         System.out.println();
113         
114         
115         System.out.println("\n第六题:");
116         NO6();
117         
118         
119         System.out.println("\n第七题:");
120         for(int i:NO7(new int[]{1,3,4,5,0,0,6,6,0,5,4,7,0,5})) System.out.print(i+" ");
121         
122     }
123 }
代码语言:javascript
复制
运行结果

第一题:
不及格的有:2个

第二题:
 6 5 4 3 2 1

第三题:
6 8 6 9 2 0 0 9 3 6 8 8 4 6 0 9 6 1 1 7 3 7 2 5 3 3 8 1 4 7 
0 出现的次数为: 3
1 出现的次数为: 3
2 出现的次数为: 2
3 出现的次数为: 4
4 出现的次数为: 2
5 出现的次数为: 1
6 出现的次数为: 5
7 出现的次数为: 3
8 出现的次数为: 4
9 出现的次数为: 3

第四题:
删除前的学生:	小明	老王	张三	赵四	小美	小娜	小华
删除后的学生:	小明	老王	赵四	小美	小娜	小华	null

第五题:
113 ere qqq yyui 

第六题:
01 张三 20 
02 李四 30 
03 王五 40 
总成绩:90

第七题:
1 3 4 5 6 6 5 4 7 5 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-10-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档