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

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

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

问题

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

以下是我的代码:

  1. package exercises;
  2. public class TimeTable {
  3. public static void main(String[] args) {
  4. int[][] nums = new int[10][2];
  5. for (int i = 0; i < 10; i++) {
  6. for (int j = 0; j < 2; j++) {
  7. nums[i][j] = (int) (Math.random() * 5 + 1);
  8. }
  9. }
  10. for (int i = 0; i < 10; i++) {
  11. System.out.println(nums[i][0] + " x " + nums[i][1] + " = ");
  12. }
  13. }
  14. }

输出结果为:

  1. 3 x 2 =
  2. 1 x 1 =
  3. 5 x 4 =
  4. 2 x 4 =
  5. 5 x 3 =
  6. 1 x 4 =
  7. 5 x 1 =
  8. 2 x 5 =
  9. 3 x 3 =
  10. 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:

  1. package exercises;
  2. public class TimeTable {
  3. public static void main(String[] args) {
  4. int[][] nums = new int[10][2];
  5. for (int i = 0; i &lt; 10; i++) {
  6. for (int j = 0; j &lt; 2; j++) {
  7. nums[i][j] = (int) (Math.random() * 5 + 1);
  8. }
  9. }
  10. for (int i = 0; i &lt; 10; i++) {
  11. System.out.println(nums[i][0] + &quot; x &quot; + nums[i][1] + &quot; = &quot;);
  12. }
  13. }
  14. }

and the output is :

  1. 3 x 2 =
  2. 1 x 1 =
  3. 5 x 4 =
  4. 2 x 4 =
  5. 5 x 3 =
  6. 1 x 4 =
  7. 5 x 1 =
  8. 2 x 5 =
  9. 3 x 3 =
  10. 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

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

  1. public static void main(String[] args) {
  2. int[][] nums = new int[10][2];
  3. for (int i = 0; i < 10; i++) {
  4. nums[i][0] = (int) (Math.random() * 5 + 1);
  5. nums[i][1] = (int) (Math.random() * 10 + 1);
  6. }
  7. for (int i = 0; i < 10; i++) {
  8. System.out.println(nums[i][0] + " x " + nums[i][1] + " = ");
  9. }
  10. }

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

  1. import java.util.Random;
  2. public class TimeTable {
  3. public static void main(String[] args) {
  4. int[][] nums = new int[10][2];
  5. Random random = new Random(/* 在这里可以放置一个种子以获取相同的序列 */);
  6. for (int i = 0; i < 10; i++) {
  7. nums[i][0] = random.nextInt(5) + 1;
  8. nums[i][1] = random.nextInt(10) + 1;
  9. }
  10. for (int i = 0; i < 10; i++) {
  11. System.out.println(nums[i][0] + " x " + nums[i][1] + " = ");
  12. }
  13. }
  14. }
英文:

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

  1. public static void main(String[] args) {
  2. int[][] nums = new int[10][2];
  3. for (int i = 0; i &lt; 10; i++) {
  4. nums[i][0] = (int) (Math.random() * 5 + 1);
  5. nums[i][1] = (int) (Math.random() * 10 + 1);
  6. }
  7. for (int i = 0; i &lt; 10; i++) {
  8. System.out.println(nums[i][0] + &quot; x &quot; + nums[i][1] + &quot; = &quot;);
  9. }
  10. }

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:

  1. import java.util.Random;
  2. public class TimeTable {
  3. public static void main(String[] args) {
  4. int[][] nums = new int[10][2];
  5. Random random = new Random(/* you can put a seed here to get identical sequences*/);
  6. for (int i = 0; i &lt; 10; i++) {
  7. nums[i][0] = random.nextInt(5) + 1;
  8. nums[i][1] = random.nextInt(10) + 1;
  9. }
  10. for (int i = 0; i &lt; 10; i++) {
  11. System.out.println(nums[i][0] + &quot; x &quot; + nums[i][1] + &quot; = &quot;);
  12. }
  13. }
  14. }

答案2

得分: 1

以下是翻译好的内容:

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

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

打印结果

  1. 3 x 2 =
  2. 1 x 3 =
  3. 4 x 10 =
  4. 1 x 9 =
  5. 3 x 5 =
  6. 5 x 4 =
  7. 1 x 1 =
  8. 2 x 4 =
  9. 2 x 10 =
  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)
  1. public class TimeTable {
  2. public static void main(String[] args) {
  3. int[][] nums = new int[2][]; // length determined by Random
  4. Random r = new Random();
  5. nums[0] = r.ints(10, 1, 6).toArray(); // 1 thru 5 inclusive
  6. nums[1] = r.ints(10, 1, 11).toArray();// 1 thru 10 inclusive
  7. for (int i = 0; i &lt; nums[0].length; i++) {
  8. System.out.printf(&quot;%d x %-2d = %n&quot;, nums[0][i], nums[1][i]);
  9. }
  10. }
  11. }

prints

  1. 3 x 2 =
  2. 1 x 3 =
  3. 4 x 10 =
  4. 1 x 9 =
  5. 3 x 5 =
  6. 5 x 4 =
  7. 1 x 1 =
  8. 2 x 4 =
  9. 2 x 10 =
  10. 1 x 1 =
  11. </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:

确定