Java 如何使这些值同时为正值和负值?
我如何让这个程序打印出负值和正值,这样就会有从-100到100的随机数Java 如何使这些值同时为正值和负值?,java,for-loop,random,Java,For Loop,Random,我如何让这个程序打印出负值和正值,这样就会有从-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; m
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.
公共类随机数{
/*
*该程序生成10个随机整数,并在控制台窗口中打印出来。
*/
公共静态void main(字符串[]args){
int[]myArray;
myArray=新整数[10];
for(int i=0;i
公共类随机化{
/*
* 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() * 200) - 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.
/*
*该程序生成10个随机整数,并在控制台窗口中打印出来。
*/
公共静态void main(字符串[]args){
int[]myArray;
myArray=新整数[10];
for(int i=0;i 对于(int i=0;i如何myArray[i]=r.nextInt()%101
其中Random r=new Random()
生成0到200之间的随机数,然后减去100。因为Math.Random()<1
,用(int)(Math.Random()*100)永远得不到100。
看一看,但这基本上是JB Nizet解释的。