I am not sure how print the values of arrays when called from methods, I have to solve these:
1- create an array consisting of 100 random integers in the range 100 to 500, including the end points. (This part i am OK, the next 2 points i am quite doubtful on how solve it)
2- make a method to print the array, 5 numbers per line, with a space between each. (I got almost everything right except I don't know how Return the value, I tried return System.outprint..... but didn't work either anyway the method has a void some made it worse)
3- make a method to print the smallest number in the array. (this i got no clue how to do it!)
- make a method to print the sum of all numbers in the array. (This I don't quite see why the "return ad;" is not working as most of the code seems correct to me at least hehe)
This is my code so far:
package randomhundred;
import java.util.Arrays;
public class RandomHundred {
private static int[] rand;
public static void main(String[] args) {
// TODO code application logic here
//setting the 100 array
/* PART I*/
int rand [] = new int [100];
int numb;
for(int i=0; i<rand.length; i++){
numb = (int) (100 + (Math.random() * ( (500 - 100) + 1)));
numb = rand[i];
}
}
/* PART II */
public static void arai (){
for (int i=0; i<100; i++){
System.out.print(rand[i] + " ");
if( i%5 == 0){
System.out.println();
}
else{
System.out.print(rand[i] + " ");
}
}
/**
PART IV
*/
public static int suma(){
int ad;
for(int i=0; i<100; i++){
ad =+rand[i];
}
return ad;
}
}
}
numb = rand[i]. You're assigning numb torand[i];, not the other way round, which is wrong. It should really berand[i] = numb;