I have a problem with arrays and assigning them to values like float. Here is my code
System.out.print("Please enter the number of iteration: ");
Scanner scn = new Scanner(System.in);
int inumber = scn.nextInt(); // i get the iteration number
Random rnd = new Random();
float x = -10 + rnd.nextFloat() * 20; // i created random number between 10 and -10
System.out.println("My x = " +x);
float[] xarray = new float[4]; // created my test array
Random arrayrnd = new Random();
for (int i=0;i<inumber;i++) { // created a for loop with that number
for (int j = 0; j<xarray.length;j++) {
xarray[j] = arrayrnd.nextFloat(); // also created random for array and assigned them
}
Arrays.sort(xarray); // i sorted the array
xarray[0] = x; // i tried to assign the smallest number to x but didn't work
System.out.println("t="+i+" My new x = " +x);
Also here is my output :
Please enter the number of iteration: 2 My x = -6.2841988 t=0 My new x = -6.2841988 t=1 My new x = -6.2841988
I just don't understand why my x is never changed even though i tried to assign the x with the new value. I want my x to change in every turn of the loop and got the the smallest number of the array. But it seems like x never want to move. I'm sorry if my code is complicated or if i have any mistakes. Happy coding!
Aucun commentaire:
Enregistrer un commentaire