0
我如何让这个程序打印出正值的负值,所以会有-100到100的随机数?如何使这些值既有正面的也有负面的?
public class RandomInts {
/*
* This program makes 10 random integers and prints them out on the console window.
*/
public static void main(String[] args) {
int[] myArray;
myArray = new int[10];
for(int i = 0; i < myArray.length; i++){
myArray[i] = (int)(Math.random() * 100);
} // End of first for loop.
for(int i=0;i<10;i++){
System.out.println("My array is " + myArray[i]);
} // End of second for loop.
} // End of the main.
} // End of the class RandomInts.
产生随机数0和200之间,再减去100 – 2014-09-23 21:31:35
因为'的Math.random()<1',你将永远不会得到100'(INT)(的Math.random ()* 100)' – 2014-09-23 21:32:13
看看http://stackoverflow.com/questions/5271598/java-generate-random-number-between-two-given-values,但它基本上是JB Nizet所解释的。 – Pheonix2105 2014-09-23 21:33:33