0

我想请求一些帮助,将此代码转换为二维数组。我不是要求修复代码,只是一个起点或其他东西,因为数组确实是我编码的弱点。这是代码:

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

3

你在寻找

String[][] twoDimensional = new String[][]{one, two, three, four, five, six};
于 2013-05-14T13:31:46.493 回答
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

正如Ahmed所建议的,您还可以将多维数据集表示为三维数组。

具有两个维度的解决方案将遵循先前答案的思路。

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)

在二维数组中,您现在有框的行​​列。或者,换句话说,你有一个盒子矩阵,或者一个矩形盒子,无论你喜欢什么。您可以通过两个索引访问各个元素。例如,a[line][column].

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

以上类似于正方形或立方体的面。

现在让我们尝试三个维度(我将无法为那个维度写出很棒的 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.
}

有了以上内容,您可以轻松访问每个小广场。
假设,在旋转时,我想改变三个右边的垂直正方形。

// 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,稍后您将想要了解枚举。

枚举允许您指定一组固定的值,以供以后使用。在您的立方体中,颜色是您事先知道的一组固定值(颜色始终是相同的六种颜色)。然后,您可以将您Color的类型指定为枚举。

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

您现在可以使用自己的颜色,而不是使用字符串。例如,让我们以上述三维数组中的旋转示例为例,并将红色分配给面零。

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

对于初学者来说,这似乎需要考虑很多,这就是为什么我把它放在我答案的不同部分的原因。

使用字符串时,如果您键入“rde”而不是“red”,您的程序将继续运行,并且您只会在为时已晚时才注意到它(,您的程序已经在运行并打印这些错误值)。

, 的主要优点enum是,如果您键入Color.RDE,您的编译器会警告您,并且在您修复它之前不会编译您的程序,这是一件好事。

于 2013-05-14T14:14:09.463 回答
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 回答