【问题标题】:How to Print Random number in a Math Table?如何在数学表中打印随机数?
【发布时间】:2019-03-22 20:36:56
【问题描述】:

我正在使用两个整数(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

并且行值大于列值,请任何人都可以帮助我吗?提前致谢。

【问题讨论】:

    标签: java random console-application


    【解决方案1】:

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

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

    到这里:

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

    Demo

    【讨论】:

    • 我需要获取行列范围之间的随机数。这和那个有关吗?
    • 感谢您的回答。它显示输出,但行值大于列值。
    • @HasithaMJayawardana 使用rand.nextInt((Math.max(i, j) - a) + 1) + a
    【解决方案2】:

    像这样改变你的随机数。

     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
    

    【讨论】:

    • 第一次检查他的编辑时间,他在 3 分钟前编辑他的答案,我在 4 分钟前回答。
    • 感谢您的回答。它显示输出但行值大于列值
    • 哪个行单元格值看起来比列值大?
    • 我修复了代码,现在可以了。谢谢你的回答。
    【解决方案3】:

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

    但你也有一个下限。您需要像以前一样将下限添加到结果中是正确的,但您需要先从范围中减去它。

    你需要改变这一行:

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

    到这里:

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

    但是您需要在检查 i

    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
    

    【讨论】:

    • 它给了我一个错误 java.lang.IllegalArgumentException: bound must be positive
    • 是的 - 当 i > j 时,您的范围变为负数。您需要将呼叫移至 if 块内的 nextInt。答案已更新。
    • 您的代码也可以正常工作。谢谢你的回答。
    【解决方案4】:

    首先,您应该知道如何在两个整数之间获取随机数,然后再编写其余的代码(检查代码 cmets)

    这是一个使用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
    

    【讨论】:

    • 您的代码也可以正常工作。谢谢你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    相关资源
    最近更新 更多