简体   繁体   English

如何用for循环产生水平和垂直线?

[英]How to produce horizontal and vertical lines with a for loop?

So this is my class. 这是我的课。 I'm trying to get the print() method to print out a horizontal line above my array and another line below my array. 我正在尝试使用print()方法在数组上方打印一条水平线,在数组下方打印另一条线。 I also want to have vertical lines between each number in my array. 我还想在数组中的每个数字之间使用垂直线。 Okay, I fixed my horizontal lines. 好吧,我固定了水平线。 Now I'm just lost on how to put vertical lines at the beginning and the end of my arrays. 现在我只是迷失了如何在数组的开头和结尾放置垂直线。

public class Array{

private int[] array;

public Array(int numElements)
{
    array = new int[numElements];
}

public void fill()
{
   for (int i = 0; i < array.length; i++)
    {
        array[i] = (int) (10*Math.random());
    } 
}

public void print()
{
    for(int x = 0; x < 2*array.length; x++)
    {
        System.out.print("-");
    }
    System.out.println();

    for(int i = 0; i < array.length; i++)
    {
        System.out.print(array[i]);
        if(i == array.length - 1)
        {
            System.out.println();
        }
        else
        {
            System.out.print("|");
        }
    }

    for(int n = 0; n < 2*array.length; n++)
    {
        System.out.print("-");

    }
    System.out.println();
}

public void sort()
{
    int n = array.length;
    boolean swapped;

    do
    {
        swapped = false;

        for(int i = 1; i < n; i++)
        {
            if(array[i-1] > array[i])
            {
                int tmp = array[i];
                array[i] = array[i-1];
                array[i-1] = tmp;
                swapped = true;
            }
        }
    } while(swapped);
}

public void printFrequency()
{
    int[] countArray = new int[10];
    for(int x : array)
    {
        countArray[x]++;
    }
    for(int i = 0; i < 10; i++)
    {
        System.out.println("There are " + countArray[i] + ", " + i + "'s ");
    }
}
}

This is my output: 这是我的输出:

Original:
-----------
1|5|9|3|3
-----------
Sorted:
-----------
1|3|3|5|9
-----------
Frequencies:
There are 0, 0's 
There are 1, 1's 
There are 0, 2's 
There are 2, 3's 
There are 0, 4's 
There are 1, 5's 
There are 0, 6's 
There are 0, 7's 
There are 0, 8's 
There are 1, 9's 

Original:
---------------------
7|0|5|2|0|1|8|0|7|7
---------------------
Sorted:
---------------------
0|0|0|1|2|5|7|7|7|8
---------------------
Frequencies:
There are 3, 0's 
There are 1, 1's 
There are 1, 2's 
There are 0, 3's 
There are 0, 4's 
There are 1, 5's 
There are 0, 6's 
There are 3, 7's 
There are 1, 8's 
There are 0, 9's 

My output should look like this: 我的输出应如下所示:

Original:
-----------
|1|5|9|3|3|
-----------
Sorted:
-----------
|1|3|3|5|9|
-----------
Frequencies:
There are 0, 0's
There are 1, 1's
There are 0, 2's
There are 2, 3's
There are 0, 4's
There are 1, 5's
There are 0, 6's
There are 0, 7's
There are 0, 8's
There are 1, 9's

Original:
---------------------
|7|0|5|2|0|1|8|0|7|7|
---------------------
Sorted:
---------------------
|0|0|0|1|2|5|7|7|7|8|
---------------------
Frequencies:
There are 3, 0's
There are 1, 1's
There are 1, 2's
There are 0, 3's
There are 0, 4's
There are 1, 5's
There are 0, 6's
There are 3, 7's
There are 1, 8's
There are 0, 9's

Move the line System.out.println(); 移动行System.out.println(); outside of your for loop if you don't want to create a new line after each dash character. 如果您不想在每个破折号后创建新行,请在for循环之外。

take the println() out of this loop : println()移出此循环:

for(int n = 0; n < array.length; n++)
{
    System.out.print("-");
    System.out.println();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM