JAVA duplex problem

Asked 2 years ago, Updated 2 years ago, 131 views

Hello, everyone I couldn't find an answer while solving the question, so I asked this question.

Write the JAVA code to multiply a given input or get an additional table!

• Note: – Use LOOP, IF/ELSE, and METHODS.

– Use the scanner class to get input from the user. Multiply operation if input operation equals 1. If the input operation equals 2, it is an additional operation. Other input numbers exit the program without performing any action.

– Use of character inputs ('*' and '+') to gain additional points. That's the question.

I coded it to make it like this ;

    int N= 6;
    int[][] array = new int[N][N];
    for(int i = 0; i < N;  i++)
    {
        for(int j = 0; j < N; j++)
        {
            array[i][j] = (int)(Math.i*j) ;
                                }
                                }

                                for(int i = 0; i < N; i++)
                                {
                                System.out.print(array[i][j] + " ");
                                }
    System.out.println();
}

I tried to make a code like this, but there is an error. I want to know which part is the problem and how to correct it to make it like this example above.

scanner multiplication java

2022-09-22 18:28

1 Answers

array[i][j] = (int)(Math.i*j); // You can't do this kind of calculation. A static field named i does not exist in the Math class. The problem is that you can simply do i * j.

Since the output is also a two-dimensional array, i.e., a matrix, the iteration must be N * M times.

for(int i = 0; i < N; i++)
{
    for(int j = 0; j < N; j++)
    {
        System.out.print(array[i][j] + " ");
    }
    System.out.println();
}

It should be printed from 1, but the code on the question will be printed from 0. Think about printing from 1.


2022-09-22 18:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.