업데이트 ****코드에서 오류가있는 루프의 경우
내 프로그램을 올바르게 컴파일하고 실행하고 있지만 다른 문제가 발생했습니다. 특정 난수가 생성 될 때마다 계산할 변수를 만들어야합니다. 예를 들어 count0
은 정수 0
이 생성 된 횟수를 기록합니다. 이것은 내가 가진 것입니다 :
import java.util.Random;
public class L10{
public static void main(String[] args){
int total = 100;
Random randObj = new Random();
final int UPPER_BOUND = 10;
for (int i=0; i < total; i++){
int randomInt = randObj.nextInt(UPPER_BOUND);
System.out.print("\n" + randomInt);
int count0 = 0;
if(randomInt==0){
System.out.print(randomInt + count0);
}
int count1 = 1;
if(randomInt==1){
}
int count2 = 2;
int count3 = 3;
int count4 = 4;
int count5 = 5;
int count6 = 6;
int count7 = 7;
int count8 = 8;
int count9 = 9;
}
}
}
결과에 임의의 숫자 (이 경우 영)가 표시되고 그 옆에 0이 인쇄됩니다. 정확히 얼마나 많은 시간이 0이 생성되었는지 추적하는 코드를 작성하는 방법을 잘 모르겠습니다. 어떤 제안?
'새로운 랜덤 (New Random) '을 사용하는 예를 살펴보고 갖고있는 것과 비교해보십시오. http://docs.oracle.com/javase/7/docs/api/java/util/Random.html –
'임의의 randObj = 새로운 임의();'? – JunM