多维数组在Java中的Math.Random – 分别限制每列的数字

huangapple go评论72阅读模式
英文:

Multi-Dimensional Arrays in Java Math.Random - Limiting numbers seperately for each column

问题

目前,我正在学习编程的过程中。我的目标是开发一个随机生成乘法问题的软件应用程序。为了实现这一目标,我希望将多维数组的第一列中生成的随机数限制在最大值为5,第二列限制在最大值为10。我已经在限制 Math.random 的范围到5或其他指定数字方面取得了一些进展,但不幸的是,这种限制适用于数组中的每一列。

以下是我的代码:

package exercises;

public class TimeTable {
    public static void main(String[] args) {
        int[][] nums = new int[10][2];

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 2; j++) {
                nums[i][j] = (int) (Math.random() * 5 + 1);
            }
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(nums[i][0] + " x " + nums[i][1] + " = ");
        }
    }
}

输出结果为:

3 x 2 = 
1 x 1 = 
5 x 4 = 
2 x 4 = 
5 x 3 = 
1 x 4 = 
5 x 1 = 
2 x 5 = 
3 x 3 = 
5 x 1 = 

从上面的示例中可以看出,乘法问题两边的数字都受到了最大值为5的限制。然而,我的目标是将第一列的最大值限制为5,同时允许第二列达到最大值10。这样可以使孩子在练习乘法时专注于特定的数字。

我已经尝试通过查看文档和搜索在StackOverflow上以前提出的问题来找到解决方案,但是尚未找到可行的解决方案。感谢您的帮助。

英文:

Currently, I am in the process of learning how to code. My goal is to develop a software application that generates multiplication questions randomly. In order to achieve this, I would like to restrict the random numbers generated in the first column of the multi-dimensional array to a maximum of 5, and to a maximum of 10 in the second column. I have made some progress in limiting the range of Math.random to 5 or any specified number, but unfortunately, this limitation applies to every column in the array.

Here is my code:

package exercises;

public class TimeTable {
    public static void main(String[] args) {
        int[][] nums = new int[10][2];

        for (int i = 0; i &lt; 10; i++) {
            for (int j = 0; j &lt; 2; j++) {
                nums[i][j] = (int) (Math.random() * 5 + 1);
            }
        }

        for (int i = 0; i &lt; 10; i++) {

            System.out.println(nums[i][0] + &quot; x &quot; + nums[i][1] + &quot; = &quot;);

        }
    }
}

and the output is :

3 x 2 = 
1 x 1 = 
5 x 4 = 
2 x 4 = 
5 x 3 = 
1 x 4 = 
5 x 1 = 
2 x 5 = 
3 x 3 = 
5 x 1 = 

From the previous example, it can be observed that the numbers on both sides of the multiplication questions are restricted to a maximum of 5. However, my objective is to limit the first column to a maximum of 5 while allowing the second column to reach a maximum of 10. This will enable children to focus on specific numbers while practicing multiplication.

I have attempted to find solutions by reviewing documentation and searching for previously asked questions on StackOverflow, but I have yet to find a viable solution. Thank you for your assistance.

答案1

得分: 2

不需要第二个循环,因为它们并不相同:

    public static void main(String[] args) {
        int[][] nums = new int[10][2];

        for (int i = 0; i < 10; i++) {
            nums[i][0] = (int) (Math.random() * 5 + 1);
            nums[i][1] = (int) (Math.random() * 10 + 1);
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(nums[i][0] + " x " + nums[i][1] + " = ");
        }
    }

此外,最好创建一个显式的Random对象,因为在这种情况下,您可以访问一个更易读的API,无需转换为int,并且在创建随机生成器时可以通过玩弄种子来创建相同的序列:

import java.util.Random;

public class TimeTable {
    public static void main(String[] args) {
        int[][] nums = new int[10][2];
        Random random = new Random(/* 在这里可以放置一个种子以获取相同的序列 */);

        for (int i = 0; i < 10; i++) {
            nums[i][0] = random.nextInt(5) + 1;
            nums[i][1] = random.nextInt(10) + 1;
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(nums[i][0] + " x " + nums[i][1] + " = ");
        }
    }
}
英文:

You don't need a second loop, since they are not doing the same:

    public static void main(String[] args) {
        int[][] nums = new int[10][2];

        for (int i = 0; i &lt; 10; i++) {
            nums[i][0] = (int) (Math.random() * 5 + 1);
            nums[i][1] = (int) (Math.random() * 10 + 1);
        }

        for (int i = 0; i &lt; 10; i++) {
            System.out.println(nums[i][0] + &quot; x &quot; + nums[i][1] + &quot; = &quot;);
        }
    }

Also it's better to create an explicit Random object, because in that case you get access to a more readable API, that does not require casting to int, and you can also create identical sequences by playing around with the seed when creating the random generator:

import java.util.Random;

public class TimeTable {
    public static void main(String[] args) {
        int[][] nums = new int[10][2];
        Random random = new Random(/* you can put a seed here to get identical sequences*/);

        for (int i = 0; i &lt; 10; i++) {
            nums[i][0] = random.nextInt(5) + 1;
            nums[i][1] = random.nextInt(10) + 1;
        }

        for (int i = 0; i &lt; 10; i++) {
            System.out.println(nums[i][0] + &quot; x &quot; + nums[i][1] + &quot; = &quot;);
        }
    }
}

答案2

得分: 1

以下是翻译好的内容:

这里有一个简单的方法来实现它。首先将数组的索引列更改为首列,然后是行。您不需要使用循环来填充数组。格式化的打印语句有助于保持'='对齐。

  • Random.ints 参数为 count, minValue, maxValue (exclusive)
public class TimeTable {
    public static void main(String[] args) {
        int[][] nums = new int[2][]; // 长度由随机数确定
        Random r = new Random();
        nums[0] = r.ints(10, 1, 6).toArray(); // 1 到 5 包括 5
        nums[1] = r.ints(10, 1, 11).toArray(); // 1 到 10 包括 10
        for (int i = 0; i < nums[0].length; i++) {
            System.out.printf("%d x %-2d = %n", nums[0][i], nums[1][i]);
        }
    }
}

打印结果

3 x 2  = 
1 x 3  = 
4 x 10 = 
1 x 9  = 
3 x 5  = 
5 x 4  = 
1 x 1  = 
2 x 4  = 
2 x 10 = 
1 x 1  = 
英文:

Here is an easy way to do it. Change your array to index columns first, then rows. You don't need a loop to fill the arrays. The formatted print statement helps keep the '=' aligned.

  • Random.ints arguments are count, minValue, maxValue (exclusive)
public class TimeTable {
    public static void main(String[] args) {
        int[][] nums = new int[2][]; // length determined by Random
        Random r = new Random();
        nums[0] = r.ints(10, 1, 6).toArray(); // 1 thru 5 inclusive
        nums[1] = r.ints(10, 1, 11).toArray();// 1 thru 10 inclusive
        for (int i = 0; i &lt; nums[0].length; i++) {
            System.out.printf(&quot;%d x %-2d = %n&quot;, nums[0][i], nums[1][i]);
        }
    }
}

prints

3 x 2  = 
1 x 3  = 
4 x 10 = 
1 x 9  = 
3 x 5  = 
5 x 4  = 
1 x 1  = 
2 x 4  = 
2 x 10 = 
1 x 1  = 

</details>



huangapple
  • 本文由 发表于 2023年3月7日 02:49:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定