So i keep getting this error message and i have no idea what it means or how to respond. Heres my code. When i go to a different compiler, i dont get this issue. Thanks in advance!
import java.math.*;
import java.util.*;
public class QuestionEight {
public QuestionEight() {
// TODO Auto-generated constructor stub
}
public static void main(String [] args) {
// TODO Auto-generated method stub
int[] newArr = new int[1000];
for(int a = 0; a<1000; a++) {
newArr[a] = countNumbers();
}
double sum = (double)sumOfArray(newArr);
double attempts = (double)1000;
System.out.println("The average is " + sum/attempts);
}
public static int countNumbers() {
int sum = 0;
int counter = 0;
while(sum<1) {
sum = Math.floor(10.0*Math.random());
counter++;
}
return counter;
}
public static int sumOfArray(int[]a) {
int[] tempArr = new int[a.length];
for(int c = 0; c<a.length; c++)
tempArr[c] = a[c];
for(int d = 1; d<a.length; d=d+2) {
tempArr[d] = tempArr[d] + tempArr[d-1];
if(d==tempArr.length-1) {
return tempArr[d];
}
}
return 0;
}
}
Your code looks fine, except that you might want to change
sum = Math.floor(10.0 * Math.random());
to
sum = (int) Math.floor(10.0 * Math.random());
since Math.floor
returns a value of type double
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.