簡體   English   中英

我如何使這些值既正面又負面?

[英]How do I make these values both positive and negative?

如何使該程序打印出負值和正值,以便從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.

你可以嘗試-

公共類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() * 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.

} //類的結尾RandomInts。

怎么樣myArray[i] = r.nextInt() % 101 ,其中Random r = new Random()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM