这是我从网上找来的练习题,花了点时间做一做,但是有些做得不太好,以后有时间再来优化吧。读者也可以自己做一做。
数组为:{1,2,3,4,5} 打印结果:1 2 3 4 5
我的代吗如下:
public static void main(String[] args) { int[] arr = new int[]{1,2,3,4,5}; for (int num:arr ) { System.out.print(num+" "); } }
double min =Double.MAX_VALUE; for(int i= 0;i<arr.length;i++){ if(min > arr[i]) { min = arr[i]; } } System.out.println("min = "+min);
我的代码如下:
Random random = new Random(); int[] arr = new int[6]; for(int i= 0;i<arr.length;i++){ arr[i] = random.nextInt(101); } int sum =0; for(int i = 0;i<arr.length;i++){ sum+=arr[i]; } System.out.println("sum = "+sum);
原数组:{1,2,3,4,5} 最终得到的结果: 12345
个人觉得这个题目有点不好,直接输出就好了
我的代码:
int[] arr = new int[]{1,2,3,4,5}; for(int i =0;i<arr.length;i++){ System.out.print(arr[i]); }
int[] arr = new int[]{72,89,65,87,91,82,71,93,76,68}; int sum = 0; for(int i =0 ;i<arr.length;i++){ sum+=arr[i]; } System.out.println("averge = "+sum/arr.length);
请输入一个整数数字: 50 生成的新数组是:12 14 23 45 50 66 68 70 77 90
代码如下:
int[] arr= new int[]{12,14,23,45,66,68,70,77,90,91}; Random random = new Random(); int insertnum = random.nextInt(1000); System.out.println("insertnum = "+insertnum); int[] newarr = new int[arr.length+1]; int flag =0; for(int i =0;i<arr.length;i++){ if(insertnum < arr[i]){ newarr[i] = insertnum; flag = 1; } if(flag == 1){ newarr[i+1] = arr[i]; } newarr[i] = arr[i]; } if(flag == 0){ newarr[newarr.length -1] =insertnum; } for (int num:newarr ) { System.out.print(num+" "); }
赚钱的股票一共有:4只 赔钱的股票一共有:6只
double[] arr = new double[]{10.4,-3,-6.2,1.2,-6.1,-19,-3.8,0.9,-4.5,5.5}; int kui=0; int zhuan = 0; for(int i =0 ;i < arr.length;i++){ if(arr[i] < 0) { kui++; } if(arr[i] > 0){ zhuan++; } } System.out.println("赚钱的股票一共有:"+ zhuan+"只"); System.out.println("赔钱的股票一共有:"+ kui+"只");
int[] arr= new int[]{1,2,3,4,5,6,7,8,9}; int[] newarr = new int[9]; int ji=0; int ou = 0; for(int i =0;i<arr.length;i++){ if( (arr[i] & 1) ==1){ ji++; }else { ou++; } } int j=0; for(int i =0;i<arr.length;i++){ if( (arr[i] & 1) ==1){ newarr[j] = arr[i]; j++; }else { newarr[ji] = arr[i]; ji++; } } for (int num:newarr ) { System.out.print(num+" "); }