#include "stdio.h"
#include "time.h"
main()
{
int a[100],i,t,x,k=0;
int m=1,n=100;
printf("该数组元素有:\n");
srand((unsigned)time(NULL));
for(i=0;i<100;i++)
{
for(t=0;t<555555;t++)
{}
a[i]=rand()%(n-m)+m;
}
for(i=0;i<100;i++)
printf("%3d",a[i]);
printf("\n输入你要查找的数值:");
scanf("%d",&x);
for(i=0;i<100;i++)
if(x==a[i])
k++;
printf("\n该数出现了%d次!\n",k);
}
注:输入100数太麻烦了,我就用系统随机生成100数,这些数在1~99之间,你想调,就去改变n的值吧!
public static void arraytest(){
int[] nums=new int[100];
for(int n=0;n<100;n++){
nums[n]=(int)(Math.random()*100);
}
Scanner sc=new Scanner(System.in);
System.out.println("请输入1-99的整数:");
int shu=sc.nextInt();
int k=0;
for(int num:nums){
System.out.println(num);
if(num==shu){
k++;
}
}
System.out.println("重复出现了:"+k+"次!");
}
main方法中直接调用
int[] ints = new int[100];
Random rd = new Random();
for (int i = 0; i < 100; i++)
{
ints[i] = rd.Next(0, 10000);
}
int intInput = 1;
int intCount = 0;
foreach (int i in ints)
{
if (i == intInput) intCount++;
}
Console.Write(intCount);