簡體   English   中英

如何在數學表中打印隨機數?

[英]How to Print Random number in a Math Table?

我正在使用兩個整數(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

並且行值大於列值,請有人可以幫助我嗎? 提前致謝。

問題是您將單元格值計算為1和列數加上行號之間的隨機數之和。 認為你想要的邏輯是矩陣中的給定單元格不能大於行數或列數的最大值 如果是這樣,那么您需要更改此行:

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

對此:

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

演示

像這樣更改你的隨機數。

 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

您想要一個可以達到上限(包括)的數字,但Random.nextInt(int)排除了上限,因此您需要為參數添加1。 要獲得從0到10(包括)的隨機數,可以使用rand.nextInt(10+1)

但你也有一個下限。 你需要像你一樣在結果中添加下限是正確的,但你需要先從范圍中減去它。

您需要更改此行:

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

對此:

int randomNum = rand.nextInt(j + 1 - 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

首先,您應該知道如何在兩個整數之間進行隨機化,然后對其余整數進行編碼(檢查代碼注釋)

這是一個使用三元運算符的實現?: ,這是很好的了解

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

暫無
暫無

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

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