如何在数学表中打印随机数?

时间:2018-10-18 04:43:42

标签: java random console-application

我正在使用两个整数(a和tableSize)实现数学表。我建立了一个名为R的随机运算。我将计算行和列范围之间的随机数并打印该随机数。对于那些行值大于列值的情况,输出为破折号(“-”)。

这是我的代码,

    int a = 4;
    int tableSize = 10;
    System.out.print("    R ");
    for(int i = a; i <= tableSize;i++ )
    {
        System.out.format("%4d",i);
    }   
    System.out.println();
    for(int i = a ;i <= tableSize;i++)
    {
        System.out.format("%4d ",i);
        for(int j=a;j <= tableSize;j++)
        {
            int randomNum = rand.nextInt (j) + i;
            if(!(i > j))
            {
                System.out.format("%4d", randomNum);
            } else
            {
                System.out.format("%4s", "-");
            }
         }
         System.out.println();
     }

我需要的输出是这样的

R  4  5  6  7  8  9  10
4  4  4  5  5  4  9   8
5  -  5  5  6  5  9   8
6  -  -  6  6  7  9   6
7  -  -  -  7  7  7   7
8  -  -  -  -  8  9   9
9  -  -  -  -  -  9  10
10 -  -  -  -  -  -  10

但是问题是我没有得到这样的输出。我收到的输出是

   R    4   5   6   7   8   9  10
   4    5   7   6   8   8  10  13
   5    -   5   9   8   8  10  12
   6    -   -   9   8  11  10  11
   7    -   -   -   8  14   9  16
   8    -   -   -   -  14  12  11
   9    -   -   -   -   -  13  18
  10    -   -   -   -   -   -  19

并且行值大于列值,请有人可以帮助我吗?预先感谢。

4 个答案:

答案 0 :(得分:5)

问题是您正在将单元格值计算为1和列号加行号之间的随机数之和。我想想的逻辑是,矩阵中的给定单元格不能大于行号或列号的 max 。如果是这样,那么您需要更改此行:

int randomNum = rand.nextInt(j) + i;

对此:

int randomNum = rand.nextInt((Math.max(i, j) - a) + 1) + a;

Demo

答案 1 :(得分:5)

像这样更改您的随机数。

 int randomNum = rand.nextInt((tableSize - a) +1)+a;

输出:

   R    4   5   6   7   8   9  10
   4    4   6   7   6   6   7   7
   5    -   6   5   4   6   8   8
   6    -   -   7   8   7   7   8
   7    -   -   -  10   7   5   5
   8    -   -   -   -   9   5   8
   9    -   -   -   -   -   8   8
  10    -   -   -   -   -   -   4

答案 2 :(得分:5)

您想要一个可以达到上限(包括上限)的数字,但是Random.nextInt(int)排除上限,因此您需要在参数中加1。要获取从零到10(含10)的随机数,可以使用rand.nextInt(10+1)

但是您也有一个下限。正确的是,您需要像往结果中那样添加结果的下限,但是您需要首先从范围中减去它。

您需要更改此行:

int randomNum = rand.nextInt (j) + i;

对此:

int randomNum = rand.nextInt(j + 1 - i) + i;

但是您需要在检查i <= j时移动此行,否则您的范围将变为负数:

if (i <= j) {
    int randomNum = rand.nextInt(j + 1 - i) + i;
    System.out.format("%4d", randomNum);
} else {
    System.out.format("%4s", "-");
}

输出:

   R    4   5   6   7   8   9  10
   4    4   5   5   7   4   5   8
   5    -   5   6   6   5   5   5
   6    -   -   6   6   8   8   9
   7    -   -   -   7   7   9   9
   8    -   -   -   -   8   9   9
   9    -   -   -   -   -   9   9
  10    -   -   -   -   -   -  10

答案 3 :(得分:4)

首先,您应该知道如何获取两个整数之间的随机数,然后对其余整数进行编码(检查代码注释)

这是一个使用ternary operator ?:的实现,很高兴知道

PrintR.java:

import java.util.Random;

public class PrintR {
    public static void main(String[] args) {
        int a = 4;
        int end = 10;
        printRTable(a, end);
    }

    public static void printRTable(int init, int end) {
        Random rand = new Random();
        // first print the top header row
        System.out.format("   R  ");
        for(int i = init; i<=end;i++ ) {
            System.out.format("%4d",i);
        }
        System.out.println();
        System.out.println("------------------------------------------");

        for(int i = init ;i<=end;i++) {
            // print left most column first
            System.out.format("%4d |",i);
            for(int j=init;j<=end;j++) {
                //Ternary operator 
                //r.nextInt(High-Low + 1) + Low; gives you a random number in between Low (inclusive) and High (inclusive)
                System.out.format("%4s", i > j ? "-" : (rand.nextInt(j-i+1)) + i);
            }
            System.out.println();
        }
    }

}

示例输出:

   R     4   5   6   7   8   9  10
------------------------------------------
   4 |   4   4   5   7   5   5   4
   5 |   -   5   6   6   5   8  10
   6 |   -   -   6   7   7   6   8
   7 |   -   -   -   7   7   7  10
   8 |   -   -   -   -   8   8  10
   9 |   -   -   -   -   -   9  10
  10 |   -   -   -   -   -   -  10