tell me how you can make a one-dimensional array of 10 numbers, and make so that all the negative numbers are replaced by -1, and all this with a graphical interface, I'm trying to do everything I reviewed the video just different articles, I wondered how to set the random generator to negative numbers, so I don’t care for it at all, but I have a simple array of Java and no more
- Negative numbers are the same positive, only negative. Subtle such hint. - Mage
- Here such import java.util.Random; public class javaapplication16 {public static void main (String [] args) {// TODO code application logic here int [] a1 = new int [10]; for (int i = 0; i <a1.length; i ++) {a1 [i] = (int) Math.round (Math.random () * 100); System.out.println (a1 [i]); } int min = 0; for (int i = 0; i <a1.length; i ++) {min = a1 [i]; for (int j = 0; j <a1.length; j ++) {if (min> a1 [j]) min = a1 [j]; }} System.out.println ("min" + min); - Ifritmm
|
2 answers
You can like this:
public static void main(String[] args) { int[] a1=new int[10]; int maxValue = 100; Random rn = new Random(); for (int i = 0; i <a1.length ; i++){ int newxValue = rn.netInt()%(maxValue+1) - maxValue/2; a1[i] = (newxValue < 0)?-1:newxValue; } } - Thank you very much, this is clearly better than what I did) - Ifritmm
|
Not the best option, but the shortest and descriptive
List<Integer> list = IntStream .generate(() -> new Random().nextInt(Integer.MAX_VALUE) - Integer.MAX_VALUE / 2) .limit(10) .mapToObj(Integer::valueOf) .collect(Collectors.toList()); |