简体   繁体   中英

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. 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. 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.

You did correctly set up 2 loops

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):

(1, 1) , (1, 2), (1, 3), (2, 1), (2, 2), and so on until (3, 3);

Obviously you want the product to be displayed, so create some variable z = x * y inside the loops

//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.

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. 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. 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!

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 + " ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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