简体   繁体   English

用户定义的Java乘法表,使用两个类文件

[英]User Defined Java Multiplication Table using Two Class Files

I need to create two class files for creating a multiplication table based upon what size the user specifies (<=15) and use the following for the assignment. 我需要根据用户指定的大小(<= 15)创建两个用于创建乘法表的类文件,并将以下内容用于赋值。 I was on Thanksgiving Break and was not able to retrieve the instructions for creating the two classes so I wrote one program that prompts the user and now I am not sure how I can break that into two classes. 我当时正处于感恩节假期,无法检索创建两个类的说明,因此我编写了一个程序来提示用户,现在我不确定如何将其分为两个类。 One class is the Table, which specifies the table size and the other class is the Table App. 一类是表格,它指定表格的大小,另一类是表格应用程序。 Here are the instructions that I didn't have access to: 以下是我无权访问的说明:

The smallest allowed table size is a 2x2 table; 允许的最小表大小是2x2的表; and the largest is a 15x15 table. 最大的是一张15x15的桌子。 The number of rows and columns will always be the same (ie, the program won't create a 5x10 table). 行数和列数将始终相同(即,程序不会创建5x10的表)。 It won't let me post a picture here but the table goes from 1 to 15 along the column headers which are separated by dashes. 它不会让我在此处发布图片,但表格沿列标题的范围从1到15,并以短划线分隔。 Then the row header is separated by dashes as it goes from 1 to 15 然后行标题从1到15时用短划线分隔

The number in each cell of the table is the product of the column heading above it, and the row label to its left. 表格中每个单元格中的数字是其上方列标题与其左侧行标签的乘积。 All of the numbers should be right-justified. 所有数字都应右对齐。

The table shall be generated by a class named Table. 该表应由名为Table的类生成。 This class can consist of as many methods as you feel it appropriate to create, but must include at least the following: 此类可以包含您认为合适的任意数量的方法,但是至少必须包括以下内容:

(constructor)--Takes one input parameter: the size of the table. (构造函数)-接受一个输入参数:表的大小。 Valid values range from 2 to 15 (inclusive). 有效值的范围是2到15(含)。 print---No input parameters and no return value. 打印---无输入参数,无返回值。 This is the method that will display the multiplication table. 这是将显示乘法表的方法。 printLine---Takes one input parameter: the number of dashes to print in line form. printLine ---采用一个输入参数:以行形式打印的破折号数。 This is a helper method that you'll use from the print method. 这是您将在print方法中使用的辅助方法。 It is for creating the three horizontal lines in the table. 它用于在表中创建三个水平线。

Without having the instructions I wrote the code to make the table a variable size and width based on the user input and did not make it inclusive to 2 to 15. I am still very new to Java and I was very proud of the code I wrote until I was able to get to the internet and see the instructions. 在没有说明的情况下,我编写了代码以根据用户输入使表具有可变的大小和宽度,并且没有使其包含2到15。我对Java还是很陌生,我为我编写的代码感到非常自豪直到我能够上网并查看说明。 This is the code that I wrote and it creates the table but I can't get the dashes perfect like the picture and I did not create two class files..I just wrote it in one. 这是我编写的代码,它创建了表格,但是我无法得到完美的短划线,如图所示,并且我没有创建两个类文件。我只是将其编写在一个文件中。 Can someone please help me?? 有人可以帮帮我吗??

import java.util.Scanner;

public class Table {
    private static Scanner s;

    public static void main(String[] args) 
    {
        s = new Scanner(System.in);

        System.out.print("How big is the table: ");

        int size = s.nextInt();
        int formatStringLength = Integer.toString(size*size).length();
        int axesFormatStringLength = Integer.toString(size).length();
        String formatString = String.format("%%%ds", formatStringLength);
        String axesFormatString = String.format("%%%ds",
                axesFormatStringLength);

        System.out.println();
        System.out.println();

        System.out.print("* | ");

        for (int i = 1; i <= size; i++) 
        {
            //System.out.print(i + " ");
            System.out.printf(formatString + " ", i);
        }

        System.out.print("\n----");

        //for (int i = 1; i <= size; i++) 
        for (int i = 1; i <= size*formatStringLength; i++) 
        {
            System.out.print("--");
        }

        System.out.println();

        for (int i = 1; i <= size; i++) {
            System.out.printf(axesFormatString + " | ", i);

            for (int j = 1; j <= size; j++) {

                System.out.printf(formatString + " ", i * j);
            }

            System.out.println();
        }
    }
}

You should really follow instruction and make two classes. 您应该真正按照说明进行两节课。 You need to create a class named Table then create a runnable application class to run use the Table class 您需要创建一个名为Table的类,然后创建一个可运行的应用程序类以使用Table类运行

Table.java Table.java

public class Table{
    private int size;

    // constructor
    public Table(int size){
        this.size = size;
    }

    public int getSize(){
        return size;
    }

    public void print(){
        // do some printing

        printline(20);

        // do some more printing

        printline(20);

        // do some more printing

        printline(20);

        // do some more printing

    }

    public void printLine(int dashes){
        // loop to print number of dashes
    }
}

TestTable.java Example run TestTable.java示例运行

public class TextTable{
    public static void main(String[] args){
        // create an instance of Table
        Table table = new Table(5);

        // print table
        table.print();
    }
}

This is a basic outline/template of what your code should look like, as per the instructions. 根据说明,这是代码外观的基本大纲/模板。

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

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