简体   繁体   English

显示不同表格内容,重复标题问题的方法

[英]Methods to display different table content, repeating header issue

I need to be able to use different methods in order to print out certain table content such as a sum, multiplication, or random table to name a few. 我需要能够使用不同的方法来打印出某些表格内容,例如总和,乘法或随机表等等。 I cannot figure out how to print out this content separate from the row ID. 我无法弄清楚如何打印出与行ID分开的内容。 another problem I am having is when I enter "f" to reprint the table the header gets repeated each time I print it which just creates a very long repeating header after a few reprints, could someone help with this? 我遇到的另一个问题是,当我输入“f”来重新打印表格时,每次打印时都会重复标题,这会在几次重印后创建一个非常长的重复标题,有人可以帮忙吗?

The original code will be followed by the IntTable class, it is not completely filled out which I am aware of but I want to figure this out before I move on. 原始代码后面是IntTable类,它没有完全填写我所知道但我想在继续之前弄清楚这一点。

import java.util.Scanner;

public class Assignment6{

    public static String returnMenu(){
        String menu = "Command Options---------------\na: Create a new table\nb: Change the row sizes\nc: Change the column sizes\nd: Change the data types\ne: Chnge the formats\nf: Display the table\ng: Display the log data\n:?: Display this menu\nq: Quit the program\n------------------------------";
        return menu;
    }
    public static void main(String[] arg){
        Scanner scan = new Scanner(System.in);
        String input = "p";
        String output = "NOOOOO!";
        IntTable myTable = new IntTable();

        while (!input.equals("q")){
            System.out.println("Please input a command: ");
            input = scan.nextLine();
            if (input.equals("a")){
                System.out.print("a [Input three integers to initialize table]: ");
                myTable.setRow(scan.nextInt()); myTable.setColumn(scan.nextInt()); myTable.setType(scan.nextInt());
                scan.nextLine();
                myTable.printTable();
            }
            else if (input.equals("b")){
                System.out.print("b [Enter integer to update table row]: ");
                myTable.setRow(scan.nextInt());
                scan.nextLine();
            }
            else if (input.equals("c")){
                System.out.println("c [Enter integer to update column]: ");
                myTable.setColumn(scan.nextInt());
                scan.nextLine();
            }
            else if (input.equals("d")){
                System.out.println("d [Enter integer to update data type]: ");
                myTable.setType(scan.nextInt());
                scan.nextLine();
            }
            else if (input.equals("e")){
                System.out.println("e [Enter integer to update printf format]: ");

            }
            else if (input.equals("f")){
                System.out.println("f [Display table]: ");
                myTable.printTable();
            }
            else if (input.equals("g")){
                System.out.println("g [Display data log]: ");
            }
            else if (input.equals("?")){
                System.out.println(Assignment6.returnMenu());
            }
            else
                System.out.println("Invalid: ***Type \"?\" to get the commands***");
        }

    }
}



public class IntTable{

    private int row, column, type;
    static String log, tFormat;
    final int TYPE_DEFAULT = 0;
    final int TYPE_NUMBERS = 1;
    final int TYPE_SUM = 2;
    final int TYPE_MULTIPLY = 3;
    final int TYPE_RANDOM = 4;
    final int TYPE_POWER = 5;
    final int TYPE_FIBONACCI = 6;

    String beginRow = "   *    |";
    String divider = "-------";
    int count = 1;
    public IntTable(){

    }

    public IntTable(int row, int column, int type){

    }
    public int getRow(){
        return row;
    }
    public int getColumn(){
        return column;
    }
    public int getType(){
        return type;
    }
    public void setRow(int newRow){
        row = newRow;
    }
    public void setColumn(int newColumn){
        column = newColumn;
    }
    public void setType(int newType){
        type = newType;
    }
    public void printTable(){
        int count = 1;



        printTableHeader();

        for(int i = 1; i <row+1; i++){
            System.out.printf("%4d %4s", i, "   |");

            for (int j = 1; j < column+1; j++)
            {
                System.out.printf("%4d", count);
                count++;
            }
            System.out.println();
        }
    }
    private void printTableHeader(){
        for(int i = 1; i < column + 1; i++)
        {
            beginRow+="   "+i;
            divider+="-----";
        }
        System.out.println(beginRow);
        System.out.println(divider);

    }
    private void printTableRowID(int ID){
        ID = row;
        for(int i = 1; i <ID+1; i++){
            System.out.printf("%4d %4s", i, "   |");
            System.out.print(getElement(i, (row-(row-1))));
            System.out.println();
        }

    }
    private int getElement(int c, int r){
        if(type == 0)
            return 0;
        else if(type == 1)
            return c+column;
        else if(type == 2)
            return c+ r;
        else if(type == 3)
            return c*r;
        else if(type == 4)
            return (int)(1 + Math.random()*10);
        else if(type == 5)
            return (int)Math.pow(r, c);
        else
            return r;
    }
    private int pow(int one, int two){
        return (int)Math.pow(one, two);
    }
    private int fibonacci(int fib){
        return fib;
    }
    private int sum(int add, int addd){
        return add + addd;
    }
    private int multiply(int prod, int product){
        return prod * product;
    }
    private int random(int what, int huh){
        return (int)(what + Math.random()*huh);
    }
}

the reason why header repeating is because you do not reinitialize variable(beginRow and divider) 标题重复的原因是因为你没有重新初始化变量(beginRow和divider)

when you call printTable Method, variable String beginRow = " * |"; String divider = "-------"; 当你调用printTable方法时,变量String beginRow = " * |"; String divider = "-------"; String beginRow = " * |"; String divider = "-------"; is redefined 被重新定义
you need to reintialize variable( beginRow , divider ) 你需要重新定义变量( beginRowdivider

look fllowing code which i modified 看看我修改的fllowing代码

private void printTableHeader() { private void printTableHeader(){

        for(int i = 1; i < column + 1; i++)
        {
            beginRow+="   "+i;
            divider+="-----";
        }
        System.out.println(beginRow);
        System.out.println(divider);

        beginRow = "   *    |";
        divider = "-------";
  } 

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

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