JAVA:有30个0-9的数字,分别统计0-9这10个数字分别出现了多少次
展开全部
public class test {
public static void main(String[] args) {
int[] a = new int[30], b = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 10); System.out.print(a[i]+",");
}
for (int i = 0; i < a.length; i++) {
b[a[i]]++;
}
System.out.println();
for (int i = 0; i < b.length; i++) {
System.out.println(i+"出现了"+b[i]+"次");
}
}
}
public static void main(String[] args) {
int[] a = new int[30], b = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 10); System.out.print(a[i]+",");
}
for (int i = 0; i < a.length; i++) {
b[a[i]]++;
}
System.out.println();
for (int i = 0; i < b.length; i++) {
System.out.println(i+"出现了"+b[i]+"次");
}
}
}
参考资料: Java API
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2019-02-16
展开全部
public static void main(String[] args) {
Random rand = new Random();
int a[] = new int[30];
for (int i = 0; i < a.length; i++) {
a[i] = rand.nextInt(9);
}
for (int j = 0; j <= 9; j++) {
int b=0;
for(int i=0;i<a.length;i++){
if(j==a[i]){
b+=1;
}
}
System.out.println("数字"+j+"出现了"+b+"次");
}
}
}
Random rand = new Random();
int a[] = new int[30];
for (int i = 0; i < a.length; i++) {
a[i] = rand.nextInt(9);
}
for (int j = 0; j <= 9; j++) {
int b=0;
for(int i=0;i<a.length;i++){
if(j==a[i]){
b+=1;
}
}
System.out.println("数字"+j+"出现了"+b+"次");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询