I'm trying to generate number's bigger than 3, i made an if statement, but this code is keep generating smaller numbers. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I'm trying to generate number's bigger than 3, i made an if statement, but this code is keep generating smaller numbers.

public class Program { public static void main(String[] args) { int myArray [] = new int [4]; int f = 20; while (f > 1) { for (int i = 0; i < myArray.length;i++){ if (f > 3 && f < 21) { myArray [i] = (int) (1+Math.random()*f); System.out.println(myArray[i]); f = f - 4; } else if (f < 3 || f > 20) { continue; } } } } }

24th Aug 2021, 5:20 PM
Endrit Metolli
3 Answers
+ 1
Some unnecessary parts of your code are: -> f<21 (no need to check for that condition since f will always be less than 21) ->the else block is useless since there is no code to run after it and it doesn't contain any useful code. Your code is generating numbers greater than 3 when I ran it. Can't figure out what you mean.
24th Aug 2021, 5:33 PM
Baribor Saturday
Baribor Saturday - avatar
0
Yeah i saw f < 21 is not necessarily after i write the code, but i could bother to delete it, the reason i made an else statement is because if number is less then 3 i don't want f to get deducted by 4.
24th Aug 2021, 5:49 PM
Endrit Metolli
0
I fix it bthw, thank you for responding!! public class Program { public static void main(String[] args) { int myArray [] = new int [4]; int f = 20; while (f > 1) { for (int i = 0; i < myArray.length;i++){ myArray [i] = (int) (1+Math.random()*f); if (myArray [i] > 3) { System.out.println(myArray[i]); f = f - 4; } } } } }
24th Aug 2021, 5:52 PM
Endrit Metolli