我如何让这个程序在二维数组中打印随机数?

时间:2014-12-09 06:14:40

标签: java arrays random

我想要一些关于取消我手动插入的数字并添加随机数的建议? 如果可以打印随机十进制数?寻找一些建议或者可能有助于二维数组的网站并没有真正得到它们。

import java.util.Scanner;

public class TwoDimensions
{
   public static void main(String[] args)
   {

      Scanner keyboard = new Scanner(System.in);
      final int ROW = 3;
      final int COLUMN = 4;
      double scores[ ][ ] = { {66.7,77.8,88.9,55.6},
                              {88.4,82.1,99.4,85.4},
                              {55.6,66.6,77.6,69.4}
                            };


       System.out.print("  ");
       for (int heading = 0; heading < scores[0].length; heading++)
         System.out.printf("%4d ",heading + 1);
       System.out.println();
       for (int row = 0; row < scores.length; row++)
       {
         System.out.printf("%d  ",row+1);
         for (int column = 0; column < scores[row].length; column++)
           System.out.printf("%4.1f ", scores[row][column]);
         System.out.println();
       }
       for (int row = 0; row < ROW; row++)
       {
          double average = 0;
          for (int column = 0; column < COLUMN; column++)
             average += scores[row][column];
          System.out.printf("Average scores for row %d is %.2f\n",
                             (row + 1), (average / COLUMN));
       }
    }
}

2 个答案:

答案 0 :(得分:0)

首先实例化一个随机对象Random generator = new Random();然后填充你的数组double scores[ ][ ] = { {generator.nextInt(100),generator.nextInt(100),generator.nextInt(100),generator.nextInt(100)}, {generator.nextInt(100),generator.nextInt(100),generator.nextInt(100),generator.nextInt(100)}, {generator.nextInt(100),generator.nextInt(100),generator.nextInt(100),generator.nextInt(100)} };注意 - 您可以根据需要更改绑定的ie100并修改您的代码以处理int而不是double如果要打印小数。

答案 1 :(得分:0)

如果我理解你的问题,这样的事情怎么样?

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

public class RandomDecimalValue {
    public static void main(String[] args) {
        final int ROW = 3;
        final int COLUMN = 4;
        double scores[][] = new double[ROW][COLUMN];

        Random randGenerator = new Random();
        Scanner sc = new Scanner(System.in);

        // Initialize 2D array with appropriate random decimal values
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COLUMN; j++) {
                scores[i][j] = (randGenerator.nextDouble() * 99.0) + 1.0;
            }
        }

        // Print out array values
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COLUMN; j++) {
                System.out.printf("%4.1f ", scores[i][j]);
            }
            System.out.println();
        }

        int randomRow = randGenerator.nextInt(ROW);
        int randomCol = randGenerator.nextInt(COLUMN);

        // Get a random decimal value from the 2D array
        System.out.println("\nRandom decimal value: ");
        System.out.printf("%-5.1f%n", scores[randomRow][randomCol]);

        // Prints out random decimal value from selected row/column
        System.out.printf("%nEnter a row number (1-%d): ", ROW);
        int selectedRow = sc.nextInt();
        System.out.printf("Enter a column number (1-%d): ", COLUMN);
        int selectedCol = sc.nextInt();

        if((selectedRow >= 1 && selectedRow <= ROW) &&
                (selectedCol >= 1 && selectedCol <= COLUMN))
        {
            System.out.printf("%nRandom decimal value at Row %d, Column %d: ",
                    selectedRow, selectedCol);
            System.out.printf("%4.1f%n",
                    scores[selectedRow - 1][selectedCol - 1]);
        }
        else
        {
            System.out.println("Invalid row/column values");
        }

        sc.close();
    }
}

<强>输出:

47.5 25.7 72.9  4.7 
95.8 88.9 49.7 43.7 
66.5 10.1 93.9 41.6 

Random decimal value: 
72.9 

Enter a row number (1-3): 1
Enter a column number (1-4): 2

Random decimal value at Row 1, Column 2: 25.7