0

What does array elements as counters in jave mean?

Every time, when I changa the number the output also changes, though I use the same numbers?

16th Jul 2020, 5:32 PM
Umidbek
Umidbek - avatar
4 Respostas
0
Can you explain same with that code snippet..?
16th Jul 2020, 6:16 PM
Jayakrishna 🇮🇳
0
import java.util.Random; public class apples { public static void main(String[] args) { Random rand = new Random(); int freq[]=new int[7]; for(int roll=1;roll<1000;roll++) { ++freq[1+rand.nextInt(6)]; } System.out.println("Face\tFrequency"); for(int face=1;face<freq.length;face++) { System.out.println(face+"\t"+freq[face]); } } }
16th Jul 2020, 6:25 PM
Umidbek
Umidbek - avatar
0
Really, I didn`t understand what is array elements as counters and in this code above if I change the number, I mean for (int face=1;.... ) to int face=0 and then again int face=1, it outputs different numbers.
16th Jul 2020, 6:29 PM
Umidbek
Umidbek - avatar
0
I really don't understanding question clearly.. Array indexes starts from 0, not 1. So your array of length 7 integers are accessed through 0 to 6 indexes but in your code your not touching freq[0] anywhere so it's value is 0 only. And actually changes value definatively because rand.nextInt(6) returns values randomly from 0 to 5, every time it different, you cannot predict. If It is not clear, then specify your expected output with small range briefly..
18th Jul 2020, 9:59 AM
Jayakrishna 🇮🇳