这个随机程序应该:
我不确定我是否正确地做了,我也不知道如何打印常见的如何...请帮助我..我的代码如下:
import java.util.ArrayList;
public class Random {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> numbers = new ArrayList<Integer>();
int counter = 0, num;
int total = 0;
do{
num=(int) (Math.random()*30);
System.out.print(num +" ");
total+=num;
numbers.add(num);
for (int i=0;i<numbers.size();i++){
if(i==num){
counter++;
}
}
}while (total<=200);
System.out.println();
System.out.println(" Numbers " +" "+ "Occurences");
System.out.println(" "+ num + " "+ counter);
}
}
答案 0 :(得分:0)
首先,在List
具体类型上使用ArrayList
接口。其次,在尝试计算出现次数之前生成整个List
。像
List<Integer> numbers = new ArrayList<>();
int total = 0;
do {
int num = (int) (Math.random() * 30);
System.out.print(num + " ");
total += num;
numbers.add(num);
} while (total <= 200);
System.out.println();
System.out.println("Numbers \t Occurences");
for (int num : numbers) {
System.out.printf("%5d \t %5d%n", num, numbers.stream()
.filter(x -> x.equals(num)).count());
}
您需要确定满足最终项目符号点的最大数量。