0

I'm making a multiplication and here is a screenshot of what I'm trying to make:

Final Product

However, I cannot get the lines to format correctly. I think I have the correct code to perform the expressions... just need help formatting the table. Is there something easy I'm missing, or do I need to restructure something?

Here is my code and below that is a screenshot of the output:

case 4:
                    System.out.print("    |");
                    for(int q=1;q<=MAX_VALUE;q++) {
                        System.out.printf("%4d",q);
                    }
                    
                    System.out.println(" ");
                    System.out.print("----+");
                    for (int u=0; u<MAX_VALUE; u++) {
                        System.out.print("----");
                    }
                    
                    System.out.println(" ");
                    
                    for(int q=1; q<=MAX_VALUE; q++) {
                        System.out.printf("%3d |",q);
                        
                        for (int m=1; m<=MAX_VALUE; m++) {
                           System.out.printf(q*m + "   ");
                           
                            
                        }
                    }    
                    System.out.println();
                break;

Not quite there

1 Answers1

0

You need that System.out.println() inside the 3rd for loop.Like so

               for(int q=1; q<=MAX_VALUE; q++) {
                    System.out.printf("%3d |",q);
                    
                    for (int m=1; m<=MAX_VALUE; m++) {
                       System.out.printf(q*m + "   ");      
                    }
                    System.out.println();
                }
            break;

You will still need formatting as you're not using 4 spaces for the output. I have added a minor width in output so try this -

System.out.print("    |");
    for(int q = 1; q <= MAX_VALUE; q++) {
        System.out.printf("%4d", q);
    }

    System.out.println(" ");
    System.out.print("----+");
    for (int u=0; u<MAX_VALUE; u++) {
        System.out.print("----");
    }

    System.out.println(" ");

    for(int q=1; q<=MAX_VALUE; q++) {
        System.out.printf("%3d |", q);

        for (int m=1; m<=MAX_VALUE; m++) {
            //System.out.printf(q*m + "   ");
            System.out.printf("%4d", q*m);
        }
        System.out.println();
    }
Prem
  • 46
  • 3