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