出于练习目的,我正在尝试找到一种方法来为每一行交替打印数字行
等。
100 99 98 97 96 95 94 93 92 91
81 82 83 84 85 86 87 88 89 90
80 79 78 77 76 75 74 73......
....不使用二维数组、ArrayLists、LinkedLists..
到目前为止,我已经做到了,但是我对此的解决方案很长并且可能会重复...想知道是否有“更短”的方法来解决这个问题...
这是我所做的...
public class PrintNumbers {
public static void main(String args[])
{
PrintNumbers app = new PrintNumbers();
app.display();
}
public void display()// main display
{
display_cell_left_to_right(100,90);
display_cell_right_to_left(81,91);
display_cell_left_to_right(80,70);
display_cell_right_to_left(61,71);
display_cell_left_to_right(60,50);
display_cell_right_to_left(41,51);
display_cell_left_to_right(40,30);
display_cell_right_to_left(21,31);
display_cell_left_to_right(20,10);
display_cell_right_to_left(1,11);
board_displayLine();
}//end display method
public void board_displayLine(){
System.out.println("");
for (int i = 10; i > 0; i--)
System.out.print("+-------");
System.out.print("+");
System.out.println("");
}//end board_displayLine
//method to display cells from left to right according to a range of indexes
public void display_cell_left_to_right(int max,int min){
board_displayLine();
for (int i = max; i > min; i--)//
System.out.print("|"+i+"\t");
System.out.print("|");
}
//same as above but vice versa
public void display_cell_right_to_left(int min, int max){
board_displayLine();
for (int i = min; i < max; i++)
System.out.print("|"+i+"\t");
System.out.print("|");
}
}
谢谢!