0

これが私のクラスです。print() メソッドを取得して、配列の上に水平線を印刷し、配列の下に別の線を印刷しようとしています。また、配列内の各数値の間に垂直線を入れたいと思っています。さて、水平線を修正しました。今、配列の最初と最後に垂直線を配置する方法がわかりません。

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

これは私の出力です:

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 

私の出力は次のようになります。

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
4

2 に答える 2

0

System.out.println();各ダッシュ文字の後に新しい行を作成したくない場合は、行を for ループの外に移動します。

于 2016-03-28T21:33:22.460 に答える
0

println()このループから抜け出します:

for(int n = 0; n < array.length; n++)
{
    System.out.print("-");
    System.out.println();
}
于 2016-03-28T21:33:35.623 に答える