2017-02-24 60 views
-2

嘿每一个我只需要格式化一些代码的一些帮助,我应该做一个表,但我似乎无法得到正确的对齐。到目前为止,这是我已经得到的代码:对齐文本没有标签 - java

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"); 
      } 
     } 

    } 
} 

到目前为止文字是这样的:Screen shot of display with code playing. 任何帮助,将不胜感激,它可以在指向正确的方向,并指出我有任何错误在代码中完成它将非常感谢!

+0

你可能想看看的System.out.format()和System.out.printf()方法。 – Ali

回答

0

最简单的方法是不使用空格。只需使用Tab标签(\ t),像这样:当你想申请

System.out.print("\t" + line); //for your top line 

现在要么éØ你可以这样做:

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

OR

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

但是,如果出于某种未知原因,您不允许使用Tab标签(\ t),那么您可以利用的String.format()方法替代选项卡标签,就像这样:

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()方法之一:/

+0

问题是我不允许使用标签功能,所以我必须弄清楚如何处理空格 – Oscarpon96

+0

@ Oscarpon96 - 请参阅我的文章中的编辑。 – DevilsHnd

0

我刚刚创建了你的程序。调整我不得不做的文字。为“O”和“E”和“O”都做类似的事情。

System.out.print("E"); 
System.out.print("  "); 
if(ii >= 10) 
{ 
    System.out.print(" "); 
} 

我用一个嵌套的循环与一堆的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"); 
} 
+0

你能解释一点吗?我不明白你的意思。 – Oscarpon96