l 有30个0~9之间的数字,分别统计0~9这10个数字分别出现了多少次
Public classTestArray
{
intscore[]={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
int num0=0;
int num1=0;
int num2=0;
int num3=0;
int num4=0;
int num5=0;
int num6=0;
int num7=0;
int num8=0;
int num9=0;
for (int i=0;i<score.length ;i++ )
{
if (score[i]==9)
{
num9++;
}
if (score[i]==1)
{
num1++;
}
if (score[i]==2)
{
num2++;
}
if (score[i]==3)
{
num3++;
}
if (score[i]==4)
{
num4++;
}
if (score[i]==5)
{
num5++;
}
if (score[i]==6)
{
num6++;
}
if (score[i]==7)
{
num7++;
}
if (score[i]==8)
{
num8++;
}
if (score[i]==0)
{
num0++;
}
}
System.out.println("0出现的次数:"+num0);
System.out.println("1出现的次数:"+num1);
System.out.println("2出现的次数:"+num2);
System.out.println("3出现的次数:"+num3);
System.out.println("4出现的次数:"+num4);
System.out.println("5出现的次数:"+num5);
System.out.println("6出现的次数:"+num6);
System.out.println("7出现的次数:"+num7);
System.out.println("8出现的次数:"+num8);
System.out.println("9出现的次数:"+num9);
}
l 定义一个整型数组,保存10个数据,利用程序完成将最大值保存在数组中的第1个元素且原第一个数换到最大值的位置!
public class TestArray01
{
publicstatic void main(String agrs[])
{
int score[]={55,45,5,44,2,6,56,15,4,12};
System.out.println("这10个数字:");
print(score);
System.out.println("");
System.out.println("改变后的数字:");
add(score);
}
publicstatic void add(int score[])
{
int max=score[0];
int arr[]=new int[score.length];
for (int i=0;i<score.length ;i++ )
{
arr[i]=score[i];
if(max<arr[i])
{
max=arr[i];
int temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
}
}
for (int i=0;i<arr.length ;i++ )
{
System.out.print(arr[i]+"、");
}
}
publicstatic void print(int arr[])
{
for (int i=0;i<arr.length ;i++ )
{
System.out.print(arr[i]+"、");
}
}
}
l 在排序好的数组中添加一个数字,将添加后的数字插入到数组合适的位置。
import java.util.Scanner;
public class TestArray02
{
publicstatic void main(String agrs[])
{
int score[]={5,10,20,40,42,100,500};
System.out.println("原数组为:");
print(score);
System.out.println("");
System.out.println("输入要插入的数字");
Scanner scan=new Scanner(System.in);
int x=scan.nextInt();
System.out.println("添加一个数字后,数组的排序为:");
add(x,score);
}
publicstatic void print(int arr[])
{
for (int i=0;i<arr.length ;i++ )
{
System.out.print(arr[i]+"、");
}
}
publicstatic void add(int y,int arr[])
{
int one[]=new int[arr.length+1];
for (int i=0;i<arr.length ;i++ )
{
one[i]=arr[i];
}
one[one.length-1]=y;
for (int i=1;i<one.length ;i++ )
{
for(int j=0;j<one.length; j++ )
{
if (one[i]<one[j])
{
inttemp=one[i];
one[i]=one[j];
one[j]=temp;
}
}
}
for (int i=0;i<one.length ;i++ )
{
System.out.print(one[i]+"、");
}
}
}
l 编写程序求1!+2!+.........30!的和并显示
Public class TestArray
{
public staticvoid main(String agrs[])
{
double i=1;
double sum=0;
double s=1;
for (i=1;i<=30 ;i++ )
{
s=s*i;
sum=sum+s;
}
System.out.print(sum);
}
}
l 定义一个由整数组成的数组,要求求出其中的奇数个数和偶数个数。
public class TestArray03
{
publicstatic void main(String agrs[])
{
int n=0;
int m=0;
int score[]={12,125,22,5,7,6,8,11,20};
for (int i=0;i<score.length ;i++ )
{
if(score[i]%2==0)
{
n++;
}else
{
m++;
}
}
System.out.println("偶数的个数:"+n);
System.out.println("奇数的个数:"+m);
}
}
l 现在定义如下的一个数组:intoldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5},要求将以上数组中值为0的去掉,将不为0的值存入一个新的数组,生成的新数组为:intnewArr[]={1,3,4,5,6,6,5,4,7,6,7,5}
public class TestArray04
{
publicstatic void main(String agrs[])
{
intoldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int k=0;
int num=0;
for (int i=0;i<oldArr.length ;i++ )
{
if(oldArr[i]==0)
{
num++;
}
}
int newArr[]=new int[oldArr.length-num];
for (int i=0;i<oldArr.length ;i++ )
{
if(oldArr[i]!=0)
{
newArr[k]=oldArr[i];
k++;
}
}
for (int i=0;i<newArr.length ;i++ )
{
System.out.print(newArr[i]+"、");
}
}
}
l //定义一个整型数组,求出数组元素的和、数组元素的最大值和最小值,并输出所求结果。
public class TestArray05
{
publicstatic void main(String agrs[])
{
int score[]={22,11,33,15,23,12,14,55};
int k=0;
int max=score[0];
int min=score[0];
for (int i=0;i<score.length ;i++ )
{
k=k+score[i];
if(max<score[i])
{
max=score[i];
}
if(min>score[i])
{
min=score[i];
}
}
System.out.println("数组的和:"+k);
System.out.println("数组的最大数:"+max);
System.out.println("数组的最小数:"+min);
}
}
l //给出10个整数(int型),然后任意查询一个数字是否存在该10个数字内
public class TestArray06
{
publicstatic void main(String[] args)
{
intscore[]={11,12,33,15,55,45,66,65,78,88};
int sum=11;
for (int i=0;i<score.length ;i++ )
{
if(sum==score[i])
{
System.out.print("sum是这10个数里的数为:"+sum);
break;
}
else
{
System.out.print("sum不是这10个数里的数!");
break;
}
}
}
}
l //定义一个包含10个元素的数组,对其进行赋值,使每个元素的值等于其下标,然后输出;最后将这个数组倒置后输出
public class TestArray07
{
publicstatic void main(String[] args)
{
int score[]=new int[10];
int arr[]=new int[score.length];
System.out.println("数组改变前:");
for (int i=0;i<score.length ;i++ )
{
score[i]=i;
System.out.print(score[i]+"、");
}
System.out.println("\n"+"数组改变后:");
for (int i=0;i<score.length ;i++ )
{
arr[i]=score[i];
}
for (int i=0;i<arr.length ;i++ )
{
score[i]=arr[score.length-1-i];
System.out.print(score[i]+"、");
}
}
}
l //给出10个老师的打分,对10个老师的打分打到最高分
public class TestArray08
{
publicstatic void main(String[] args)
{
intscore[]={100,33,15,95,11,23,54,23,45,86};
int max=score[0];
for (int i=0;i<score.length ;i++ )
{
if(max<score[i])
{
max=score[i];
}
}
System.out.println("老师打的最高分:"+max);
}
}
转载于:https://blog.51cto.com/liuyj/1656007