无制表符对齐文本-java

无制表符对齐文本-java,java,spaces,Java,Spaces,嘿,每个人,我只是需要一些帮助格式化一些代码,我应该做一个表,但我似乎不能得到正确的对齐方式。到目前为止,这是我得到的代码: public class assignment2 { public static void main(String[] args) { int line = 0; int down = 0; int num =0; for (line = 1; line < 21; line++){ System.ou

嘿,每个人,我只是需要一些帮助格式化一些代码,我应该做一个表,但我似乎不能得到正确的对齐方式。到目前为止,这是我得到的代码:

 public class assignment2 {
   public static void main(String[] args) { 
    int line = 0;
    int down = 0;
    int num =0;


    for (line = 1; line < 21; line++){ 
        System.out.print("     " + line );  //prints the numbers across with 5 spaces

    } 
        System.out.print("\n");  //moves the compiler to a new line, same thing as System.out.println 


    for (down = 1; down <= 20; down++) { //prints out the numbers down with 4 spaces to the first number 
        System.out.print(down + "    "); 
        for (int i = 1; i <= 20; i++){ //Counter to stand in place for the x-axis numbers
            num = i % down; 
            if (num != 0) { 
                System.out.print("    "); // 5 spaces, checks to see if the number are multiples of each other 
                if (i > 9){
                    System.out.print(" " ); //1 spaces, also aligns the numbers going diagonally 
                }
                } else {
                if (i >= 10){
                    if (i % 2 == 0 ) { 

                        System.out.print("E"+"      "); // 6 spaces 
                    } else  
                        System.out.print("O"+ "      "); // 6 spaces

                } else {
                    if (i % 2 == 0 ) { 
                        System.out.print("E"+"     "); // 5 spaces
                    } else { 
                        System.out.print("O"+ "     "); // 5 spaces

                    }
                } 
            }
            if (down >=2){
                System.out.print(" "); // 1 space 
            }
            if (i == 20){
                System.out.print("\r\n");
            }
        } 

    } 
}
到目前为止,文本如下所示:
如果您能指出正确的方向并指出我在代码中所犯的任何错误,我们将不胜感激

最简单的方法是根本不使用空格。只需使用Tab标签\t,如下所示:

System.out.print("\t" + line);  //for your top line
现在,当您想要应用E或O时,您可以执行以下操作:

System.out.print("\tE");

但是,如果由于未知原因不允许您使用Tab标记,\t则可以使用String.format方法代替Tab标记,如下所示:

System.out.print(String.format("%"+spaces+"s", "E"));
int line, down, num, spaces;
for (line = 1; line <= 25; line++){ 
    System.out.print(String.format("%6s", line));  
} 
System.out.print("\n");  
for (down = 1; down <= 25; down++) { 
    System.out.print(down); 
    for (int i = 1; i <= 25; i++){ 
        num = i % down;
        if (down > 0 && down < 10 && i == 1) { spaces = 5; }
        else if (down > 9 && i == 1) { spaces = 4; }
        else { spaces = 6; }
        if (num != 0) { 
            System.out.print(String.format("%"+spaces+"s", " ")); 
        } 
        else {
            if (i % 2 == 0 ) { 
                System.out.print(String.format("%"+spaces+"s", "E"));
            } 
            else { 
                System.out.print(String.format("%"+spaces+"s", "O"));
            }
        }
    }
    System.out.println("");
}
其中spaces变量将容纳要在文本中留下的空格数。任务的可能格式副本可能如下所示:

System.out.print(String.format("%"+spaces+"s", "E"));
int line, down, num, spaces;
for (line = 1; line <= 25; line++){ 
    System.out.print(String.format("%6s", line));  
} 
System.out.print("\n");  
for (down = 1; down <= 25; down++) { 
    System.out.print(down); 
    for (int i = 1; i <= 25; i++){ 
        num = i % down;
        if (down > 0 && down < 10 && i == 1) { spaces = 5; }
        else if (down > 9 && i == 1) { spaces = 4; }
        else { spaces = 6; }
        if (num != 0) { 
            System.out.print(String.format("%"+spaces+"s", " ")); 
        } 
        else {
            if (i % 2 == 0 ) { 
                System.out.print(String.format("%"+spaces+"s", "E"));
            } 
            else { 
                System.out.print(String.format("%"+spaces+"s", "O"));
            }
        }
    }
    System.out.println("");
}

但是……再说一遍……您可能也不允许使用String.format方法://

我刚刚创建了您的程序。要对齐文本,我必须这样做。对O和E或O都不存在的情况做类似的处理

System.out.print("E");
System.out.print("     ");   
if(ii >= 10)
{
    System.out.print(" ");
}
我使用一个嵌套的for循环和一组if-else语句来完成程序:

例如:

for(int i = 0; i <= numberEntered; i++)
{
    for(int ii = 0; ii <= numberEntered; ii++)
    {
         //A bunch of code here
    }
    System.out.print("\n");
}

您可能想看看System.out.format和System.out.printf方法。问题是我根本不允许使用tab函数,所以我必须找出如何使用空格