简体   繁体   English

我需要对当前代码执行什么操作才能使程序运行乘法表(使用二维数组)?

[英]What do I need to do to my current code to make the program run a multiplication table (using a two dimensional array)?

I'm trying to make a Java program that uses the input value from a user to calculate and list the products of two numbers up to the entered number. 我正在尝试制作一个Java程序,该程序使用用户的输入值来计算和列出两个数字的乘积,直到列出的数字为止。 Like if a user enters 2, the program should calculate the products between the two numbers (1 *1, 1*2, 2*1, 2*2) stores the products in a two-dimensional array, and list the products. 就像用户输入2一样,程序应计算两个数字(1 * 1、1 * 2、2 * 1、2 * 2)之间的乘积,并将乘积存储在二维数组中,并列出乘积。 I'm not sure that I totally understand arrays and so I feel as though my code is problem not right in many instances, can someone please tell me what I should do to my current code to make it work properly. 我不确定我是否完全理解数组,因此我觉得我的代码在很多情况下都不是正确的问题,有人可以告诉我我应该如何处理当前代码以使其正常工作。 Thanks in advance! 提前致谢! :) :)

import java.util.Scanner; 

public class ProductTable {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String inputString;
    char letter = 'y'; 


    // Prompt the user to enter an integer
    while(letter != 'q') {
    System.out.print("Enter a positive integer: "); 
    int integer = input.nextInt();

   // Create an two-dimensional array to store products 
   int[][] m = new int[integer][3];
   for (int j = 1; j <= m.length; j++) {
      m[j][0] = input.nextInt();
      m[j][1] = input.nextInt();
      m[j][2] = input.nextInt();
   }

   // Display the number title
   System.out.print("   ");
   for (int j = 1; j <= m.length; j++)
      System.out.print("   " + j);

   System.out.println("\n--- ");

   // Display table body
    for (int i = 1; i <= m.length + 1; i++) {
       System.out.print(i);
       for (int j = 1; j <= m.length + 1; i++) {
          System.out.printf("%4d", i * j); 

       }
       System.out.println();
    }



        // Prompt the user to either continue or quit
    System.out.print("Enter q to quit or any other key to continue: ");
    String character = input.nextLine();
    inputString = input.nextLine();
    letter = inputString.charAt(0);
    }  

  }
}     

You can achieve your multiplication table by iterating over 2 counter variables as you already did in your output 您可以像在输出中一样通过迭代2个计数器变量来实现乘法表

// Display table body
// loops running out of bounds (until m.length + 1 instead of m.length-1)
for (int i = 1; i <= m.length + 1; i++) {
   System.out.print(i);
   for (int j = 1; j <= m.length + 1; i++) { // missed to increment j here
      System.out.printf("%4d", i * j); 

   }
   System.out.println();
}

Arrays should be based on 0, that means an array with 3 fields has the indexes 0, 1, 2. So the last index is length-1. 数组应基于0,这意味着具有3个字段的数组的索引为0、1、2。因此最后一个索引为length-1。 Your condition <=m.length+1 runs out of bounds. 您的条件<=m.length+1超出范围。 index<length will work since 2 is less than 3, but not 3 less than 3. index<length将起作用,因为2小于3,但3小于3。

You have also a typo: In the inner loop you are doing an increment of i instead of j , so the loop will run infinite. 您还有一个错字:在内部循环中,您将i而不是j进行增量,因此循环将无限运行。

Try to create an outer and inner loop as you did, but with start index 0 and end condition index<inputValue . 尝试创建外部循环和内部循环,但要使用起始索引0和结束条件index<inputValue Calculate multiTable[index1][index2] = (1+index1)*(1+index2) . 计算multiTable[index1][index2] = (1+index1)*(1+index2)

Then do a similar loop and print the array fields. 然后执行类似的循环并打印数组字段。 You don't need to use an array, you could output directly as you did. 您不需要使用数组,可以像直接一样直接输出。 But you wanted to practice handling arrays. 但是您想练习处理数组。


Since you want to learn and understand, I don't like just to write the code. 因为您想学习和理解,所以我不只是编写代码。 imagine an array with 3 fields having the indexes 0, 1, 2: 想象一个具有3个字段的数组,其索引为0、1、2:

index 0 | 1 | 2
value 1 | 2 | 3

Now you would check the length at first, that's 3. You start with 0 and count while the counter does not reach the length since the zero based index is 1 below our natural order (0,1,2 vs. 1,2,3). 现在,您将首先检查长度,即3。从0开始并进行计数,而计数器达到长度,因为从零开始的索引比我们的自然顺序(0,1,2与1,2,3低1) )。 That is the case as long the index is below the length, meaning index<length . 只要索引在长度以下,就是这种情况,即index<length


Try this logic (pseudo code). 试试这个逻辑(伪代码)。 To simplify the problem we include the 0 in our product table. 为了简化问题,我们在产品表中添加了0 So we get 0*0, 0*1, 0*2... Doing so, we do not have to ignore index 0 or to calculate between index 0 should represent value 1. 因此,我们得到0 * 0、0 * 1、0 * 2 ...这样,我们不必忽略索引0或计算索引0之间的值应该表示值1。

maxNumber = userInput()

outer loop idx1 from 0 to maxNumber
  inner loop idx2 from 0 to maxNumber
    array[idx1][idx2] = (idx1) * (idx2)
  end loop
end loop

Do the same to dump your generated array to screen. 进行同样的操作以将生成的数组转储到屏幕上。

First get it running . 首先运行它 Afterwards you could try to alter the logic, so that it shows you only numbers from 1 to max. 之后,您可以尝试更改逻辑,以便仅显示从1到最大的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图按字母顺序对数组排序。 当我运行程序时,它从不交换名称。 我需要做什么来修复程序? - Im trying to sort an Array Alphabetically. When i run the program it never swaps the names. What do i need to do to fix my program? 如何让我的二维数组在 java 中水平和垂直显示? - How do I make my two-dimensional array show horizontally as well as vertically in java? 如何让我的程序只运行第二个输出? - How do I make my program run only the second output? 如何打印二维数组? - How do I printout my 2 dimensional array? 如何在二维数组中留下某种标记,该标记会告诉程序在该位置执行某些操作 - How can I leave some kind of a mark in two-dimensional array that will tell the program to do something in this location 我在代码中需要什么来检查数组中的元素是否与之​​前的相同 - What do I need here in my code to check if an element in an array is the same with the previous 我如何使这个数组乘法表起作用 - How do I get this array multiplication table to work 使用二维数组创建和打印乘法表? - Creating and printing a multiplication table using a two dimensional arrays? 如何在二维ArrayList上添加元素/数据? - how do I add elements/data on my two dimensional ArrayList? 如何复制最后带有 null 的二维数组? - How do I copy a two dimensional array with null at the end?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM