简体   繁体   中英

Java - Create a multiplication table using strings

So I recently got an this assignment in class and need help, I've checked out other questions but couldn't find one that was similar to what my teacher was asking for. She is pretty strict and want's things exactly how they're asked for.

"Create a class called MultiplicationTable and MultiplicationTableTester. A multiplication table has a max.

The Multiplication table should have method called String createMultiplcationTable() that creates a multiplication table.

Use “\\n” to obtain a new line, the program should use the string “\\t” to tab to the next tab position, so that the information is displayed neatly in the columns. The MultiplicationTableTester class should prompt the user for the max (don't allow the user to enter negative number, if they do continue prompting them for a valid value until one is entered)."

This is my horrible attempt at doing this

/* MultiplicationTable.java*/

public class MultiplacationTable {
private int maxNum;
private int i = 1;

public MultiplacationTable(int n) {
    Scanner in = new Scanner(System.in);
    maxNum = in.nextInt();
    n = maxNum;
}

public String createMultiplacationTable() {
        String row = "";
        String col = "";
        String tmpRow= "";
        String tmpCol = "";
        String table = "";

    while (i < maxNum) {
        tmpRow = "" + i;
        i++;
        row += tmpRow + "\t";

        tmpCol = "" + i;
        i++;
        col += tmpCol + "/n";
            for (int j = 1;  j < maxNum; j++) {
                System.out.print(" " + i * j);
    }
            table = row + col;
    }
    return table;
 }
}

Didn't know what to do with the tester besides print out the method

/*MultiplicationTableTester*/

public class MultiplacationTableTester {

public static void main (String[] args) {

    System.out.print("Please input a number: ");

     MultiplacationTable mT = new MultiplacationTable(0);
     System.out.print(mT.createMultiplacationTable());
  }
}

My output using 5 as input is

Please input a number: 5
3 6 9 12 5 10 15 201    3   2/n4/n

So obviously really wrong. I have a feeling what I'm doing wrong has to do with the "/n" and "\\t". Any help?

change "/n" with "\\n"

public String createMultiplacationTable() {
    String row = "";
    String col = "";
    String tmpRow= "";
    String tmpCol = "";
    String table = "";

while (i < maxNum) {
    tmpRow = "" + i;
    i++;
    row += tmpRow + "\t";

    tmpCol = "" + i;
    i++;
    col += tmpCol + "\n";
        for (int j = 1;  j < maxNum; j++) {
            System.out.print(" " + i * j);
}
        table = row + col;
}
return table;

} }

I didn't test it but it should work. Don't hesitate to ask if you don't understand anything. I hope it's clean and nice enough.

Multiplication Table Tester

public class MultiplicationTableTester {

public static void main (String[] args) {
    int maxNum;
    System.out.println("Please enter a number: ");

    Scanner in = new Scanner(System.in);
    maxNum = in.nextInt();

    MultiplicationTable mT = new MultiplicationTable(maxNum);

    System.out.print(mT.createMultiplicationTable());
  }
}

MultiplicationTable.java

public class MultiplicationTable {
private int maxNum;

public MultiplicationTable(int maxNum) {
    this.maxNum = maxNum;
}

public String createMultiplicationTable() {
    StringBuilder table = new StringBuilder();

    for(int i = 1; i<=maxNum;i++){
       for(int j = 1; j<=10; j++){                            
          table.append(i*j);
          table.append("\t"); 
       }
       table.append("\n");
    }

    return table.toString();
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM