English 中文(简体)
利用 lo
原标题:Grid using loops
  • 时间:2011-11-20 21:05:24
  •  标签:
  • java

I m making a program that creates a grid with a number of lines and columns specified by the user. I want the output to look something like this:

段 次 页 次 3月

Columns (2..20)? 5

   |   |   |   |

---+---+---+---+---

   |   |   |   |

---+---+---+---+---

   |   |   |   |

但我的方案却这样做:

段 次 页 次 3月

Columns (2..20)? 5

   |   |   |   |

---+---+---+---+---

   |   |   |   |

---+---+---+---+---

   |   |   |   |

---+---+---+---+---

这是该方案所期望的:

String[] vertical = new String[columns-1];
String[] horizontal = new String[columns];

do
{
    for(i = 0; i < vertical.length; ++i)
    {
        vertical[i] = ("   |");
        System.out.print(vertical[i]);
    }

    System.out.printf("%n");

    for(i = 1; i < columns; ++i)
    {
        horizontal[0] = ("---");
        horizontal[i] = ("+---");
    }

    if(horizontal.length > 0)
        System.out.print(horizontal[0]);

    for(int m = 1; m < horizontal.length; ++m)
        System.out.print(horizontal[m]);

    System.out.printf("%n");
    ++j;
} while (j <= lines - 1);

I know that I need to remove an horizontal line, the problem is that I don t know how to. And sorry for the bad formatting.

最佳回答

你们来到这里......,如果是的话,你们的法典是一件好事。

    String[] horizontal = new String[columns-1];
    String[] minusplus = new String[columns];

    int j = 0;

    for(int i = 1; i < columns; ++i)
        {
            minusplus[0] = ("---");
            minusplus[i] = ("+---");
        }

    for(int i = 0; i < horizontal.length; ++i)
        {
            horizontal[i] = ("   |");
        }

    do
    {
        for(int i = 0; i < horizontal.length; ++i)
        {
            System.out.print(horizontal[i]);
        }

        System.out.printf("%n");


        if ( j < lines - 1 ){
        for(int m = 0; m < minusplus.length; ++m)
            System.out.print(minusplus[m]);
        }

        System.out.printf("%n");

        ++j;
    } while ( j < lines);

EDIT: Here you have a way better implementation:

int columns = 5;
int lines = 3;

    int j = 0;
    do {
        for (int i = 0; i < columns - 1; i++) {
            System.out.print("   |");
        }

        System.out.printf("%n");

        if (j < lines - 1) {

            System.out.print("---");
            for (int i = 1; i < columns; i++) {
                System.out.print("+---");
            }
        }
        System.out.printf("%n");

        ++j;
    } while (j < lines);
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签