在這段代碼中,我生成了 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
uj5u.com熱心網友回復:
看起來您可以只使用嵌套回圈。
for(int i = 0; i < 10; i ){
for(int j = 0; j < 10; j ){
/* write code to add a single value */
}
}
uj5u.com熱心網友回復:
您可以使用兩個回圈并且只生成整數來獲取隨機值。
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");
}
uj5u.com熱心網友回復:
正如 Ariel 提到的,您可以使用嵌套的 for 回圈將 的值存盤tile在 2D 陣列中。
由于我們總共有 100 個值,并且我們希望將它們按 10 分組,因此我們應該有一個 10 x 10 的 2D 陣列來存盤Strings:
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;
}
}
emotions當我們的回圈從 0-0 的 ij 對變為 9-9 的 ij 對時,這將允許我們填滿。
現在,我們還可以使用 for-each 回圈列印出我們的陣列以輸出這些值:
for(String[] i: emotions){
for(String j: i){
System.out.print(j " ");
}
System.out.println();
}
這段代碼的作用是:“對于i我們的二維陣列中的每個子陣列,以及子陣列中emotions的每個字串,我們都可以列印出來。在列印出所有字串之后,我們可以列印出換行符并移動到我們的下一個子陣列。jijjiiemotions
總體而言,代碼應如下所示:
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();
}
我希望這有幫助!如果您需要任何進一步的說明或詳細資訊,請告訴我:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/433002.html
