Java数组填充
Java array filling
在这段代码中,我生成了 100 个具有不同值(欲望、愤怒...)的图块,并将它们打印成 10 组,每组 10 个元素。
问题是我想在 10x10 网格中打印每个图块值。
如何用所需的图块值填充示例网格(随附)?
设置网格的格式:
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
//For loop to generate 100 tiles
for(int i = 0; i <= 100; i++){
//Generate a number between 0 - 100 (based on percentages) to find which tile to place
double percentChanceRandom1 = r.nextDouble(100);
double percentChanceRandom2 = r.nextDouble(100);
double percentChanceRandom3 = r.nextDouble(100);
double percentChanceRandom4 = r.nextDouble(100);
double percentChanceRandom5 = r.nextDouble(100);
//System.out.println(percentChanceRandom);
//Determine the tile square value
if(percentChanceRandom1 <= percentChanceLust){
tile = "Lust";
}
else if(percentChanceRandom2 <= percentChanceAnger){
tile = "Anger";
}
else if(percentChanceRandom3 <= percentChanceIdol){
tile = "Idolatry";
}
else if(percentChanceRandom4 <= percentChanceVanity){
tile = "Vanity";
}
else if(percentChanceRandom5 <= percentChanceSpeech){
tile = "Speech";
}
else {
tile = "Physical";
}
if(i%10 == 0){
System.out.println(tile);
System.out.println("\n");
}
//Print out which tile was chosen
else{
System.out.println(tile);
}
}//End For
看来你只能使用嵌套循环了。
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
/* write code to add a single value */
}
}
您可以使用两个循环,只生成整数来获得随机值。
List<String> values = Arrays.asList("Lust", "Anger", "Idolatry", "Vanity", "Speech", "Physical");
Random rand = new Random()
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
System.out.print(values.get(rand.nextInt(values.size())));
}
System.out.print("\n");
}
正如 Ariel 提到的,您可以使用嵌套的 for 循环将 tile
的值存储在二维数组中。
由于我们总共有 100 个值,并且我们希望将它们按 10 分组,因此我们应该有一个 10 x 10 的二维数组,用于存储 String
s:
String[][] emotions = new String[10][10];
要遍历 emotions
并将其填满,我们可以使用嵌套的 for 循环,其中一个计数器遍历“宽度”,另一个遍历“高度”:
for(int i = 0; i < 10; j++){
for(int j = 0; j < 10; j++){
//defining tile
emotions[i][j] = tile;
}
}
当我们的循环从 i-j 对 0-0 到 i-j 对 9-9 时,这将允许我们填充 emotions
。
现在,我们还可以使用 for-each 循环打印数组来输出这些值:
for(String[] i: emotions){
for(String j: i){
System.out.print(j + " ");
}
System.out.println();
}
这段代码的作用是:“对于我们的二维数组 emotions
中的每个 sub-array i
,以及我们 [=46= 中的每个字符串 j
] i
,我们可以打印出j
,当我们打印出i
中的所有字符串j
后,我们可以打印出一个换行符,继续我们的下一个sub-array i
在 emotions
.
总体而言,代码应如下所示:
String[][] emotions = new String[10][10];
//For loop to generate 100 tiles
for(int i = 0; i < 10; j++){
for(int j = 0; j < 10; j++){
double percentChanceRandom1 = r.nextDouble(100);
double percentChanceRandom2 = r.nextDouble(100);
double percentChanceRandom3 = r.nextDouble(100);
double percentChanceRandom4 = r.nextDouble(100);
double percentChanceRandom5 = r.nextDouble(100);
//System.out.println(percentChanceRandom);
//Determine the tile square value
if(percentChanceRandom1 <= percentChanceLust){
tile = "Lust";
}
else if(percentChanceRandom2 <= percentChanceAnger){
tile = "Anger";
}
else if(percentChanceRandom3 <= percentChanceIdol){
tile = "Idolatry";
}
else if(percentChanceRandom4 <= percentChanceVanity){
tile = "Vanity";
}
else if(percentChanceRandom5 <= percentChanceSpeech){
tile = "Speech";
}
else {
tile = "Physical";
}
emotions[i][j] = tile;
}
}//End For
for(String[] i: emotions){
for(String j: i){
System.out.print(j + " ");
}
System.out.println();
}
希望对您有所帮助!如果您需要任何进一步的说明或详细信息,请告诉我:)
在这段代码中,我生成了 100 个具有不同值(欲望、愤怒...)的图块,并将它们打印成 10 组,每组 10 个元素。
问题是我想在 10x10 网格中打印每个图块值。
如何用所需的图块值填充示例网格(随附)?
设置网格的格式:
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
//For loop to generate 100 tiles
for(int i = 0; i <= 100; i++){
//Generate a number between 0 - 100 (based on percentages) to find which tile to place
double percentChanceRandom1 = r.nextDouble(100);
double percentChanceRandom2 = r.nextDouble(100);
double percentChanceRandom3 = r.nextDouble(100);
double percentChanceRandom4 = r.nextDouble(100);
double percentChanceRandom5 = r.nextDouble(100);
//System.out.println(percentChanceRandom);
//Determine the tile square value
if(percentChanceRandom1 <= percentChanceLust){
tile = "Lust";
}
else if(percentChanceRandom2 <= percentChanceAnger){
tile = "Anger";
}
else if(percentChanceRandom3 <= percentChanceIdol){
tile = "Idolatry";
}
else if(percentChanceRandom4 <= percentChanceVanity){
tile = "Vanity";
}
else if(percentChanceRandom5 <= percentChanceSpeech){
tile = "Speech";
}
else {
tile = "Physical";
}
if(i%10 == 0){
System.out.println(tile);
System.out.println("\n");
}
//Print out which tile was chosen
else{
System.out.println(tile);
}
}//End For
看来你只能使用嵌套循环了。
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
/* write code to add a single value */
}
}
您可以使用两个循环,只生成整数来获得随机值。
List<String> values = Arrays.asList("Lust", "Anger", "Idolatry", "Vanity", "Speech", "Physical");
Random rand = new Random()
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
System.out.print(values.get(rand.nextInt(values.size())));
}
System.out.print("\n");
}
正如 Ariel 提到的,您可以使用嵌套的 for 循环将 tile
的值存储在二维数组中。
由于我们总共有 100 个值,并且我们希望将它们按 10 分组,因此我们应该有一个 10 x 10 的二维数组,用于存储 String
s:
String[][] emotions = new String[10][10];
要遍历 emotions
并将其填满,我们可以使用嵌套的 for 循环,其中一个计数器遍历“宽度”,另一个遍历“高度”:
for(int i = 0; i < 10; j++){
for(int j = 0; j < 10; j++){
//defining tile
emotions[i][j] = tile;
}
}
当我们的循环从 i-j 对 0-0 到 i-j 对 9-9 时,这将允许我们填充 emotions
。
现在,我们还可以使用 for-each 循环打印数组来输出这些值:
for(String[] i: emotions){
for(String j: i){
System.out.print(j + " ");
}
System.out.println();
}
这段代码的作用是:“对于我们的二维数组 emotions
中的每个 sub-array i
,以及我们 [=46= 中的每个字符串 j
] i
,我们可以打印出j
,当我们打印出i
中的所有字符串j
后,我们可以打印出一个换行符,继续我们的下一个sub-array i
在 emotions
.
总体而言,代码应如下所示:
String[][] emotions = new String[10][10];
//For loop to generate 100 tiles
for(int i = 0; i < 10; j++){
for(int j = 0; j < 10; j++){
double percentChanceRandom1 = r.nextDouble(100);
double percentChanceRandom2 = r.nextDouble(100);
double percentChanceRandom3 = r.nextDouble(100);
double percentChanceRandom4 = r.nextDouble(100);
double percentChanceRandom5 = r.nextDouble(100);
//System.out.println(percentChanceRandom);
//Determine the tile square value
if(percentChanceRandom1 <= percentChanceLust){
tile = "Lust";
}
else if(percentChanceRandom2 <= percentChanceAnger){
tile = "Anger";
}
else if(percentChanceRandom3 <= percentChanceIdol){
tile = "Idolatry";
}
else if(percentChanceRandom4 <= percentChanceVanity){
tile = "Vanity";
}
else if(percentChanceRandom5 <= percentChanceSpeech){
tile = "Speech";
}
else {
tile = "Physical";
}
emotions[i][j] = tile;
}
}//End For
for(String[] i: emotions){
for(String j: i){
System.out.print(j + " ");
}
System.out.println();
}
希望对您有所帮助!如果您需要任何进一步的说明或详细信息,请告诉我:)