2

I want to output the power table from n (0) to n (10) with just the base being inputted using the scanner.

For now, I am having difficulties in setting it up

Code:

else if (option == 2){
                int base = keyboard.nextInt();
                for (int x = base; x <= base; x++){
                    System.out.print(base+"^");
                    for (int y = 0; y <= 10; y++){ // "y" is exponent
                        System.out.print(y+"=");
                }
                System.out.println("");
            }
        }

Sample input:

  2
  5

Expected Output:

     5^0=
     5^1=
     5^2=
     5^3=
     - - - several lines are skipped here - - -
     5^10=

Note: This is not the expected output, but I want to try out the code by myself this is just the step that would take me to the end result

  • 1
    HussainOmer123 - I just answered a similar question as your edited question [here](https://stackoverflow.com/questions/62050129/how-do-i-make-my-answer-print-correctly-in-java/62050547#62050547). – Arvind Kumar Avinash May 27 '20 at 18:51

1 Answers1

1

Problems in your code:

  1. You have used result inside the loop and therefore it is getting reset with each iteration.
  2. You have put the print statement inside the loop
  3. You need to multiply result with base with each iteration which is not done.

Do it as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int option = keyboard.nextInt();
        int base = keyboard.nextInt();
        int exponent = keyboard.nextInt();
        int result = 1;
        if (option == 1) {
            for (int x = base; x <= base; x++) {
                if (exponent <= 1) {
                    System.out.print(base + "^" + exponent);
                } else {
                    System.out.print(base + "^" + exponent + "=");
                }
                for (int y = 1; y <= exponent; y++) {
                    System.out.print(exponent == 1 ? "" : (base + (y < exponent ? "*" : "")));
                    result *= base;
                }
                System.out.print("=" + result);
            }
        }
    }
}

A sample run:

1
2
5
2^5=2*2*2*2*2=32

[Update]

As per your comment, you haven't learnt ternary operator but I strongly recommend you learn it. The solution given below is without using the ternary operator:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int option = keyboard.nextInt();
        int base = keyboard.nextInt();
        int exponent = keyboard.nextInt();
        int result = 1;
        if (option == 1) {
            for (int x = base; x <= base; x++) {
                if (exponent <= 1) {
                    System.out.print(base + "^" + exponent);
                } else {
                    System.out.print(base + "^" + exponent + "=");
                }
                for (int y = 1; y <= exponent; y++) {
                    if (y < exponent) {
                        System.out.print(base + "*");
                    } else if (exponent != 1) {
                        System.out.print(base);
                    }
                    result *= base;
                }
                System.out.print("=" + result);
            }
        }
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/214638/discussion-on-answer-by-arvind-kumar-avinash-how-to-use-nested-loops-to-output-p). – Samuel Liew May 26 '20 at 04:28
  • Hello Arvind, could you take a look at this question? https://stackoverflow.com/questions/64144653/how-to-change-colors-of-a-circle-using-a-timer-in-a-java-gui –  Sep 30 '20 at 20:43
  • 1
    @HussainOmer - Check [this](https://stackoverflow.com/questions/64143096/im-trying-to-print-out-prime-number-but-not-sure-whats-wrong-with-my-code/64143167#comment113431358_64143167). – Arvind Kumar Avinash Sep 30 '20 at 21:04
  • Hello Arvind, could you answer this: https://stackoverflow.com/questions/64266140/how-to-make-the-program-to-check-the-instance-of-a-letter-in-java Its not GUI but more with arrays –  Oct 08 '20 at 16:34