利用随机产生100个整数给一维数组赋值输入任意一个整数查找该数在数组中是否存在并统计出现次数

java
2025-05-12 21:18:26
推荐回答(2个)
回答1:

import java.util.Scanner;

public class TestOld {
public static void main(String[] args) {
int[] a = new int[100];
int count = 0, hcount = 0;
for(int i = 0; i < a.length; i++){
a[i] = (int) (99 * Math.random() + 1);
}
System.out.println("输出随机生成的数组: ");
for(int t : a){
System.out.print(t + " ");
hcount++;
if(hcount == 10){
System.out.println();
hcount = 0;
}
}
Scanner sc = new Scanner(System.in);
System.out.println("输入一个整数: ");
int temp = sc.nextInt();
for(int i = 0; i < a.length; i++){
if(temp == a[i]){
count++;
}
}
if(count == 0){
System.out.println("该数不存在这个数组中");
}else{
System.out.println("该数在数组中出现[" + count + "]次");
}
}
}

回答2:

int(rand() * 900) + 100