0

このコードを 2 次元配列に変換する方法を教えてください。私はコードの修正を求めているのではなく、単なる出発点か何かを求めているのではありません。なぜなら、配列はコーディングにおける私の弱点だからです。コードは次のとおりです。

import java.io.*;
import java.util.*;

public class rubix
{
    public static void main(String[] args)
    {
        String[] one = {"red","red","red","red","red","red","red","red","red"};
        String[] two = {"blue","blue","blue","blue","blue","blue","blue","blue","blue"};
        String[] three = {"yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow"};
        String[] four = {"green","green","green","green","green","green","green","green","green"};
        String[] five = {"orange","orange","orange","orange","orange","orange","orange","orange","orange"};
        String[] six = {"white","white","white","white","white","white","white","white","white"};

        //Output each side of the rubix cube
        output(one, 1);
        output(two, 2);
        output(three, 3);
        output(four, 4);
        output(five, 5);
        output(six, 6);

    }

    //Output function, will output first the num

    public static void output(String[] side, int num)
    {
        int i,j;
        int x = 0;
        System.out.println("Side: "+num);

        for(i = 0; i < 3; i++)
        {
            for(j = 0; j < 3; j++)
            {
                System.out.print(side[x]+"\t");
                x++;
            }
            System.out.println();

        }

        System.out.println();
        System.out.println();

    }
}
4

4 に答える 4

1
String a[][]={
        {"red","red","red","red","red","red","red","red","red"},
        {"blue","blue","blue","blue","blue","blue","blue","blue","blue"},
        {"yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow"},
        {"green","green","green","green","green","green","green","green","green"},
        {"orange","orange","orange","orange","orange","orange","orange","orange","orange"},
        {"white","white","white","white","white","white","white","white","white"}       
};

// some examples    
System.out.println(a[0][0]); // red    
System.out.println(a[3][0]); // green 
于 2013-05-14T13:37:59.137 に答える
0

3 次元配列を探している場合は、次を確認してください。

public static void main(String[] args) {
    String[][][] rubik={
            {
                {"red","red","red"},
                {"red","red","red"},
                {"red","red","red"}
            },{             
                {"blue","blue","blue"},
                {"blue","blue","blue"},
                {"blue","blue","blue"}
            },{
                {"yellow","yellow","yellow"},
                {"yellow","yellow","yellow"},
                {"yellow","yellow","yellow"}
            },{
                {"green","green","green"},
                {"green","green","green"},
                {"green","green","green"}
            },{
                {"orange","orange","orange"},
                {"orange","orange","orange"},
                {"orange","orange","orange"}
            },{
                {"white","white","white"},
                {"white","white","white"},
                {"white","white","white"}
            }
    };

    output(rubik, 0);
    output(rubik, 1);
    output(rubik, 2);
    output(rubik, 3);
    output(rubik, 4);
    output(rubik, 5);
}

public static void output(String[][][] rubik, int num)
{
    int i,j;
    int x = 0;
    System.out.println("Side: "+num);

    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            System.out.print(rubik[num][i][j]+"\t");
            x++;
        }
        System.out.println();

    }

    System.out.println();
    System.out.println();

}

少し明確にするために:

rubik[s][c][r]

s=side
c=column on the side s
r=row on the side s 
于 2013-05-14T13:54:01.393 に答える
0

Ahmedが示唆しているように、キューブを 3 次元配列として表現することもできます。

2次元のソリューションは、以前の回答に沿ったものになります。

String[][] cube = {
    {"red","red","red","red","red","red","red","red","red"},
    {"blue","blue","blue","blue","blue","blue","blue","blue","blue"},
    {"yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow"},
    {"green","green","green","green","green","green","green","green","green"},
    {"orange","orange","orange","orange","orange","orange","orange","orange","orange"},
    {"white","white","white","white","white","white","white","white","white"}
}

oneこれにより、two、 などの配列が置き換えられます。

配列を理解するのはそれほど難しくありません。
箱を想像してみてください。それはあなたの毎日の変数になります。String s、例になります。

Awesome ASCII variable representation:
    [«content»]

この例えでは、配列は、インデックスがゼロのボックスの行になります。つまり、その行 (配列length) でいくつのボックスが結合されているかをプログラムに伝えてから、個々のボックスに番号 (たとえば、 ) でアクセスしますa[index]

Awesome ASCII array representation:
    [«content»][«content»][«content»] ... [«content»]
       Box 0      Box 1      Box 2       Box (length-1)

2 次元配列では、ボックスの行と列があります。または、言い換えると、ボックスのマトリックス、またはボックスの四角形など、好きなものがあります。個々の要素には 2 つのインデックスでアクセスします。たとえば、a[line][column].

Awesome ASCII matrix representation:
    Lines/Columns   0    1    2    3    4    ...
                0  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                1  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                2  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                3  [ ]  [ ]  [ ]  [ ]  [ ]   ...
                4  [ ]  [ ]  [ ]  [ ]  [ ]   ...
               ...

上記は正方形、または立方体の面に似ています。

3 次元で試してみましょう (3 次元の素晴らしい ASCII アートを書くことはできません)。

String[][][] cube = {
    // First face, a square, or a two-dimensional array
    {
       // First line
       {"red", "red", "red"},
       // Second line
       {"red", "red", "red"},
       // Third line
       {"red", "red", "red"}
    },
    // Second face
    {
       // First line
       {"blue", "blue", "blue"},
       // Second line
       {"blue", "blue", "blue"},
       // Third line
       {"blue", "blue", "blue"}
    },
    // Do the same for the four remaining faces.
}

上記により、すべての小さな正方形に簡単にアクセスできます.
回転時に、3 つの垂直方向の正方形を変更したいとします。

// For face f (0 .. 5), change 3rd column (2), in every line (0, 1, 2).
cube[f][0][2] = newcolor;
cube[f][1][2] = newcolor;
cube[f][2][2] = newcolor;

さらに興味がある場合は、読み続けてください。これがニーズに合っている場合は、ここで読むのをやめてください。


この質問の範囲には含まれていませんが、Java に固執する場合は、後で列挙型について学びたいと思うでしょう。

列挙を使用すると、後で使用するために固定の値セットを指定できます。立方体では、色は事前にわかっている固定値のセットです (色は常に同じ 6 色です)。Colorその後、列挙型としてタイプを指定できます。

public enum Color {
    RED, BLUE, ORANGE, GREEN, YELLOW, WHITE
}

文字列を使用する代わりに、独自の色を使用できるようになりました。たとえば、上記の 3 次元配列の回転の例を取り上げて、赤色を面 0 に割り当ててみましょう。

cube[0][0][2] = Color.RED;
cube[0][1][2] = Color.RED;
cube[0][2][2] = Color.RED;

これは、初心者にとっては多くのことを理解するように思えるかもしれません。そのため、これを回答の別のセクションに入れました.

文字列を使用する場合、「red」の代わりに「rde」と入力すると、プログラムは続行されますが、既に手遅れになった場合にのみ気付くでしょう (つまり、プログラムは既に実行されており、これらの誤った値を出力しています)。

, の主な利点はenum、 と入力するColor.RDEと、コンパイラが警告を発し、それを修正するまでプログラムをコンパイルしないことです。これは便利なことです。

于 2013-05-14T14:14:09.463 に答える