我想请求一些帮助将此代码转换为二维数组。我不是要求修复代码,只是一个起点或什么,因为数组真的是我编码的弱点。这是代码:
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();
}
}
答案 0 :(得分:3)
您在寻找
吗?String[][] twoDimensional = new String[][]{one, two, three, four, five, six};
答案 1 :(得分: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
答案 2 :(得分:0)
也许您正在寻找一个三维数组,请检查以下内容:
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
答案 3 :(得分: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)
在二维数组中,您现在拥有行和列的行。或者,换句话说,你有一个矩阵的盒子,或者一个矩形的盒子,无论你喜欢什么。您可以通过两个索引访问各个元素。对于isntance,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”而不是“红色”,你的程序就会继续,只有当它已经太晚时才会注意到它( ie ,你的程序已经在运行并打印这些错误的值。)
enum
的主要优点是,如果你键入Color.RDE
,你的编译器会警告你,并且在你修复之前不会编译你的程序,这是一件好事。