简体   繁体   English

使用循环在Java中制作基本乘法表

[英]Making a basic multiplication table in java using loop

Hi I am a beginner so please don't use any complicated stuff. 嗨,我是初学者,所以请不要使用任何复杂的东西。 Here is a SS of what i mean. 这是我的意思的SS。 http://prntscr.com/1ffec0 as of now don't worry about having numbers vertically and horizontally that display what columns and row number it is. 到目前为止, http://prntscr.com/1ffec0不必担心垂直和水平方向的数字会显示它是什么列和行号。 I have my code but am totally confused on how to go about it and how to make them multiply. 我有我的代码,但对如何执行以及如何使其倍增感到完全困惑。

import java.util.Scanner;

public class test 
{
    public static void main(String[] args)    
    {
        Scanner keyboard = new Scanner (System.in);
        int x=0, y=0;
        System.out.print("Enter rows ");
        x = keyboard.nextInt();
        System.out.print("Enter columns ");
        y = keyboard.nextInt();

        for (int i=1; i<=x; i++) 
        {
            for (int j=1; j<=y; j++)
            {
                System.out.print(" "+i+j);
            }
            System.out.println(); 
        }
    }
}

I'm not going to give you the answer straight out, but I'm going to help you understand the problem better with some pseudocode. 我不会直接给您答案,但是我会通过一些伪代码来帮助您更好地理解问题。 Assume that your x-range goes from 1 to 3, and your y-range also goes from 1 to 3. 假设您的x范围从1到3,并且您的y范围也从1到3。

You did correctly set up 2 loops 您正确设置了2个循环

Loop x = 1 to 3
    Loop y = 1 to 3
        //Do stuff
    End innerloop
End outerloop

Now consider the values that will be printed at //do stuff, in pairs like (x, y): 现在考虑将在// do东西上打印的值,成对出现(x,y),如:

(1, 1) , (1, 2), (1, 3), (2, 1), (2, 2), and so on until (3, 3); (1,1),(1、2),(1、3),(2、1),(2、2)等,直到(3,3);

Obviously you want the product to be displayed, so create some variable z = x * y inside the loops 显然,您希望显示产品,因此在循环内创建一些变量z = x * y

//Do stuff:
z = x * y
Print z + " "

Print z out and leave a space, because you want to print the next value without it being adjacent to the first value. 打印出z并留一个空格,因为您要打印下一个值而不使其与第一个值相邻。

This will print all your solutions in a straight, single line. 这将在一条直线上打印所有解决方案。 But you want it to be in a matrix obviously. 但是您显然希望它在矩阵中。 The answer is to simple a simple change, taking only one line of code. 答案是简单的简单更改,仅需一行代码。 After each full cycle of your inner loop, you essentially finish one row of multiplication (think about why). 在内部循环的每个完整周期之后,您基本上完成了一行乘法(思考为什么)。 So the solution is that after your inner loop finishes running, right before going to the next outer loop value for x, you want to print a new line. 因此解决方案是,在您的内循环完成运行之后,就在转到下一个x的外循环值之前,您要打印新行。 All in all we have something like: 总而言之,我们有这样的东西:

Loop x = 1 to 3
    Loop y = 1 to 3
        z = x * y
        Print z + " "
    End innerloop
    Print NewLine // "\n" is the way to do that
End outerloop

And you're done. 这样就完成了。 You just need to put it into code, as well as accept user input instead of hard coding the range as from 1 to 3, or whatever. 您只需要将其放入代码中,并接受用户输入即可,而不是对范围从1到3或任何其他范围进行硬编码。 This is trivial and I'm sure you'll be able to put it together. 这是微不足道的,我相信您将能够将其组合在一起。

将i + j更改为i * j顺便说一句,您仅打印矩阵的一半

You need to use "*" instead of "+" ? 您需要使用“ *”代替“ +”吗?

Like this: 像这样:

public static void print(int x, int y) {
    for (int i = 1; i <= x; i++) {

        for (int j = 1; j <= y; j++) {

            System.out.print(" " + i * j);
        }
        System.out.println();

    }
}

And after that you might want to think about the formatting!? 然后,您可能要考虑格式!! My advice: think about the length of the longest value (is always x*y) and "reservice" enought space for it! 我的建议:考虑最长值的长度(始终为x * y),并为其“预留”足够的空间!

Change: 更改:

System.out.print(" "+i+j);

To: 至:

if ((i + j) <= 9) {
    System.out.print(i + j + "   ");
} else if ((i + j) <= 99) { 
    System.out.print(i + j + "  ");
} else 
    System.out.print(i + j + " ");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM