简体   繁体   English

Java-使用字符串创建乘法表

[英]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. “创建一个名为MultiplicationTable和MultiplicationTableTester的类。一个乘法表的最大值为

The Multiplication table should have method called String createMultiplcationTable() that creates a multiplication table. 乘法表应具有名为String createMultiplcationTable()的方法,该方法可创建乘法表。

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. 使用“ \\ n”获取新行,程序应使用字符串“ \\ t”制表到下一个制表符位置,以便信息在各列中整齐地显示。 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)." “ MultiplicationTableTester类应提示用户输入最大值(如果用户确实继续提示他们输入有效值,直到输入一个为止,则不允许用户输入负数)。”

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 我的输出使用5作为输入是

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". 我觉得我做错了与“ / n”和“ \\ t”有关。 Any help? 有什么帮助吗?

change "/n" with "\\n" 用“ \\ n”更改“ / 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 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();
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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