fork(3) download
  1.  
  2. public class Main {
  3.  
  4. public static void main(String[] args) {
  5. int[] array = new int[20];
  6. for(int i=0; i<20; i++) {
  7. array[i] = new java.util.Random().nextInt(3);
  8. }
  9. // for(int i=0; i<20; i++) {
  10. // System.out.println(array[i]);
  11. // }
  12.  
  13. int counter_0 = 0;
  14. int counter_1 = 0;
  15. int counter_2 = 0;
  16. for(int i=0; i<20; i++) {
  17. if(array[i]==0) {
  18. counter_0 += 1;
  19. } else if(array[i]==1) {
  20. counter_1 += 1;
  21. } else { // array[i]==2 の時
  22. counter_2 += 1;
  23. }
  24. }
  25. System.out.println("0の個数は"+counter_0+"、 1の個数は"+counter_1+"、 2の個数は"+counter_2);
  26.  
  27. }
  28.  
  29. }
  30.  
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
0の個数は5、 1の個数は11、 2の個数は4