Question while writing Java code.

Asked 2 years ago, Updated 2 years ago, 24 views

Hi, how are you?

I'm a coding beginner who recently started Java.

First, enter a constant.

Depending on the input of * and +,

Like this or

I tried to make the result come out like this.

That's why I coded it I keep getting errors and it's not being completed.

Which part is wrong

Which part do you want to know

I'd appreciate it if you could tell me if I should fix it.

private static Scanner console;
private static Scanner console2;

public static void main(String[] args) {
    console = new Scanner(System.in);
    console2 = new Scanner(System.in);
    System.out.print("Input the number: ");
    int n = console.nextInt();
     System.out.print("Input the operation:");
     int m = console2.nextInt();;

     if (m = '*') {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.printf("%3d ", i*j);
            }System.out.println();
        }

     }
      if(n='+')  
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.printf("%3d ", i+j);
            }System.out.println();
    }
}

}

java

2022-09-22 14:18

1 Answers

Do it like this.

import java.util.Scanner;

public class TestEverything {
    private static Scanner console;
    private static Scanner console2;

    public static void main(String[] args) {
        console = new Scanner(System.in);
        console2 = new Scanner(System.in);
        System.out.print("Input the number: ");
        int n = console.nextInt();
        System.out.print("Input the operation:");
        String m = console2.next();

        if ("*".equals(m)) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    System.out.printf("%3d ", i * j);
                }
                System.out.println();
            }

        }
        if ("+".equals(m)) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    System.out.printf("%3d ", i + j);
                }
                System.out.println();
            }
        }
    }
}


2022-09-22 14:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.