Question

I have an assignment due tomorrow, I finished it, I just keep getting one error. Well its not exactly an error as much as it is my output is not the output my teacher wants.

My teacher wants me to create a multiplication table using JOptionPane

And this is what I have so far:

import javax.swing.JOptionPane;

public class assign3 {

public static void main(String[] args) {

        String Boundary1 = JOptionPane.showInputDialog(null, "Please enter the first boundary of the multiplication table.");
        String Boundary2 = JOptionPane.showInputDialog(null, "Please enter the second boundary of the multiplication table.");

        int X = Integer.parseInt(Boundary1);
        int Y = Integer.parseInt(Boundary2);
        int j = 1;
        String Result = "";
        int x = 1;

        while (x <= X){
            for(int i = 1;i<= Y; i++ ){
                j = i * x;
                Result = Result + j + "    ";
            }
            x++;
            Result = Result + "\n";
        }
        JOptionPane.showMessageDialog(null, Result);
    }
}

My output shows up like this.

https://www.dropbox.com/s/ulp1gj9sqi94d3a/IMG_20140114_162054.jpg

But my teacher wants the output to show up like this.

https://www.dropbox.com/s/6gtexqoj3rs7xvl/IMG-20140114-WA0000.jpg

My code does not have the correct spacing between the digits, I have been trying to fix it somehow for sometime with no luck.

Était-ce utile?

La solution

One solution(assumes result already has needed tabs(\t)):

JTextArea jf=new JTextArea(result);

jf.setEditable(false);
jf.setOpaque(false);

JOptionPane.showMessageDialog(null, jf);

EDIT:

public static void main(String[] args) {

        String Boundary1 = JOptionPane.showInputDialog(null, "Please enter the first boundary of the multiplication table.");
        String Boundary2 = JOptionPane.showInputDialog(null, "Please enter the second boundary of the multiplication table.");

        int X = Integer.parseInt(Boundary1);
        int Y = Integer.parseInt(Boundary2);
        int j = 1;
        String Result = "";
        int x = 1;

        while (x <= X) {
            for (int i = 1; i <= Y; i++) {
                j = i * x;
                Result = Result + j + "\t";
            }
            x++;
            Result = Result + "\n";
        }
        JTextArea jt=new JTextArea(Result);
        jt.setEditable(false);
        jt.setOpaque(false);
        jt.setTabSize(3);
        JOptionPane.showMessageDialog(null, jt);
    }

O/P: enter image description here

Autres conseils

Have you tried using String.format("%5d", j) ?

It will print each int on 5 spaces - length.

Example: 10 will be ___10.

Or String.format("%-5d", j) to align to left.

Example: 10___.

Edit: Another option is to use html:

public static void main(String[] args) {

    String Boundary1 = JOptionPane.showInputDialog(null, "Please enter the first boundary of the multiplication table.");
    String Boundary2 = JOptionPane.showInputDialog(null, "Please enter the second boundary of the multiplication table.");

    int X = Integer.parseInt(Boundary1);
    int Y = Integer.parseInt(Boundary2);
    int j = 1;
    String Result = "<html><table>";
    int x = 1;
    while (x <= X){
        Result += "<tr>";
        for(int i = 1;i<= Y; i++ ){

            j = i * x;
            Result += String.format("<td><center>%d</center></td>", j);
        }
        x++;
        Result = Result + "</tr>";
    }

    Result += "</table></html>";

    JOptionPane.showMessageDialog(null, Result);

}

Result:

Result

Solved

    public class assign3 {

    public static void main(String[] args) {

        String Boundary1 = JOptionPane.showInputDialog(null, "Please enter the first boundary of the multiplication table.");
        String Boundary2 = JOptionPane.showInputDialog(null, "Please enter the second boundary of the multiplication table.");

        int X = Integer.parseInt(Boundary1);
        int Y = Integer.parseInt(Boundary2);
        int j = 1;
        String Result = "    |"+"\t";
        int x = 1;

        for(int i=1;i<=Y;i++){
                Result = Result + i + "\t";
            }
            Result=Result+"\n";
            while (x <= X){
            if(x<10){
            Result=Result+ x + "  |" +"\t";
            }else{
            Result=Result+ x + "|" +"\t";

            }
            for(int i = 1;i<= Y; i++ ){
                j = i * x;
                Result = Result + j +"\t";
            }
            x++;
            Result = Result + "\n";
        }
        JTextArea jt=new JTextArea(Result);
        jt.setEditable(false);
        jt.setOpaque(false);
        jt.setTabSize(3);
        JOptionPane.showMessageDialog(null, jt);        
    }
    }

OUTPUT:

output

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top