嘿每一個我只需要格式化一些代碼的一些幫助,我應該做一個表,但我似乎無法得到正確的對齊。到目前爲止,這是我已經得到的代碼:對齊文本沒有標籤 - 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. 任何幫助,將不勝感激,它可以在指向正確的方向,並指出我有任何錯誤在代碼中完成它將非常感謝!
你可能想看看的System.out.format()和System.out.printf()方法。 – Ali