我需要将随机生成的数字插入数组中

时间:2015-06-24 03:05:16

标签: java

我用相似的词语或意图仔细查看了这些问题,但似乎找不到能帮助我的问题。

我使用用户输入创建了一个数组,我需要生成100到10000之间的数字并将其插入到数组中。我已经创建了数组,它显示为填充了零,并且我生成了随机数,但我似乎无法将随机生成的数字插入到数组中。

到目前为止,这是我的代码。

import java.util.*;

public class Print2DArray {

    public static void main(String[] args) {

        System.out.println();
        Scanner userInput = new Scanner(System.in);

        System.out.println("Enter the number of rows");
        int rows = userInput.nextInt();

        System.out.println("Enter the number of columns");
        int columns = userInput.nextInt();

        int[][] array = new int[rows][columns];
        for (int a = 0; a < rows; a++)
            for (int b = 0; b < columns; b++)
                array[a][b] = 0;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(array[i][j]);
                {
                    Random r = new Random();
                    int rand = r.nextInt(10000 - 100) + 100;
                }
            }
            System.out.println();
        }
    }
}

4 个答案:

答案 0 :(得分:4)

import java.util.Random;
import java.util.Scanner;

public class QuickTester {

    public static void main(String[] args) {

        Scanner userInput = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = userInput.nextInt();

        System.out.print("Enter the number of columns: ");
        int columns = userInput.nextInt();

        // Create the Random instance once here!
        Random rand = new Random();
        int[][] array = new int[rows][columns];
        for (int a = 0; a < rows; a++)
            for (int b = 0; b < columns; b++)
                array[a][b] = rand.nextInt(10000 - 100) + 100;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }   
    }
}

<强>输入/输出:

Enter the number of rows: 3
Enter the number of columns: 2
574 5286 
1550 5259 
8343 4877

注意:

  • 只创建一次Random,而不是在double for loop
  • 的每次迭代中创建
  • 如果您想要100到10000(含),则需要nextInt(10000-100+1) + 100
  • Random#nextInt(int n)给你[0,9990]或[0,9989]所以你从表达式得到的最大值是9999,而不是10000

答案 1 :(得分:1)

// 1.  Try focusing out of control.
ie.TextField(Find.ById("NewCardOrAccountNumber")).TypeText("9440385200600000020");
ie.TextField(Find.ById("OldCardOrAccountNumber")).Click();
ie.WaitForComplete();

// 2. Try Using Send Keys method to tab out.
ie.TextField(Find.ById("NewCardOrAccountNumber")).TypeText("9440385200600000020");
System.Windows.Forms.SendKeys.SnedWait("{TAB}"); // Need to add System.Windows.Forms reference to the project.

答案 2 :(得分:1)

您没有将rand分配给数组元素。 你的第二个循环应该是

/**
 * Job Schema
 */
var JobSchema = new Schema({
    title: { type: String, required: true },
    location: { type: String, required: true },
    geoLocation: { type: [Number], index: '2d' },
    company: { type: String, required: true },
    description: { type: String, required: true },
    skillTags: [{ type: Schema.ObjectId, ref: 'Tag', index: true, required: true }],
    roleTags: [{ type: Schema.ObjectId, ref: 'Tag', index: true, required: true }]
}, {
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
});

答案 3 :(得分:1)

你需要做3件事,

  • 将随机值分配到数组中。

  • 仅使用一个嵌套的import java.util.Scanner; public class PigLat{ public static void main(String [] args) { Scanner scanner = new Scanner(System.in); String text, pigLatin; char first; System.out.print("Enter a line of text: "); text= scanner.nextLine(); first = text.charAt(0); if (first == 'a' || first == 'e' || first =='i'|| first == 'o' || first == 'u') pigLatin = text + "-way"; else pigLatin = text.substring(1) + "-" + text.charAt(0) + "ay"; System.out.println("Input : " + text); System.out.print("Output: " + pigLatin); } }

  • 不要在循环内初始化for loop

试试这个:

Random r