好吧,所以我有一個保齡球遊戲分數計算器,並且它有一個主類,它從命令行讀取輸入併爲每個玩家創建一個「PlayerScore」對象,PlayerScore對象具有一個陣列列表和一個添加到該陣列列表的方法,遊戲應該逐幀進行,並在玩家之間交替進行,要求他們在每一幀中擊倒的針的數量,然後計算並生成格式化的記分表,例如我輸入了會有兩個玩家,然後我將玩家名稱存儲在一個String ArrayList中,然後將一個空的玩家核心對象存儲在一個單獨的對應的PlayerScore Arraylist中,我有一個for循環,它嵌套在for循環中跟蹤當前幀,所以當我輸入一個玩家的分數時,應該參考他們的playerscore對象,這裏是我的代碼:Java保齡球遊戲 - 參考不同的對象
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
/*
* In each iteration call the add method of each players PlayerScore object
*/
public class BowlingGameCalculator {
static int Frame = 1;
private static String res;
static ArrayList<PlayerScore> playerArrays = new ArrayList<PlayerScore>();
//static HashMap<String, PlayerScore> play = new HashMap<String, PlayerScore>();
static ArrayList<String> newscore = new ArrayList<String>();
static ArrayList<String> players = new ArrayList<String>();
static String[] scores = new String[21];
static int rscore;
static ArrayList<Integer> running_score = new ArrayList<Integer>();
public static void main(String[] args) throws IOException {
System.out.println("Enter the number of bowlers");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for(int i =0; i<num; i++) {
System.out.println("enter bowlers name:");
Scanner inp = new Scanner(System.in);
String input = inp.next();
PlayerScore scr = new PlayerScore();
// play.put(input, scr);
players.add(input);
playerArrays.add(scr);
}
System.out.println(players);
outerloop:
for(int j=0; j<players.size();) {
for(int k=0; k< 10;) {
if(j > players.size()-1) {
j = j-players.size();
Frame++;
}
System.out.println("Frame : " + Frame);
if(Frame == 11) {
break outerloop;
}
String playr = players.get(j);
System.out.println("enter rolls for " + playr);
System.out.println("roll 1 : ");
Scanner inp = new Scanner(System.in);
int input = inp.nextInt();
if(input == 10) {
PlayerScore score = playerArrays.get(j);
System.out.println(score);
score.addRoll(input);
score.show();
// PlayerScore scr = play.get(j);
// scr.addRoll(input);
j++;
}
else {
System.out.println("roll 2 :");
Scanner inp2 = new Scanner(System.in);
int input2 = inp2.nextInt();
PlayerScore score = playerArrays.get(j);
System.out.println(score);
score.addRoll(input);
score.addRoll(input2);
score.show();
// PlayerScore scr = play.get(playr);
// scr.addRoll(input);
//scr.addRoll(input2);
// System.out.println(scr);
// scr.show();
//System.out.println(play);
j++;
}
}
//System.out.println(play);
}
}
}
這裏是PlayerScore類
import java.util.ArrayList;
public class PlayerScore {
static ArrayList<Integer> score = new ArrayList<Integer>();
static ArrayList<Integer> newscore = new ArrayList<Integer>();
static ArrayList<Integer> running_score = new ArrayList<Integer>();
public static int rscore;
public PlayerScore() {
}
public PlayerScore(int[] in) {
}
public void addRoll(int x) {
score.add(x);
}
//public boolean isComplete(){
//}
//public int getScore(){
//}
public ArrayList<Integer> show() {
System.out.println(this.score);
return this.score;
}
現在這裏是從試運行
Enter the number of bowlers
2
enter bowlers name:
matt
enter bowlers name:
derp
[matt, derp]
Frame : 1
enter rolls for matt
roll 1 :
5
roll 2 :
5
[email protected]
[5, 5]
Frame : 1
enter rolls for derp
roll 1 :
5
roll 2 :
5
[email protected]
[5, 5, 5, 5]
Frame : 2
enter rolls for matt
roll 1 :
好輸出,所以你可以看到它增加了軋輥[5, 5]添加到matt的playscore對象中的arraylist,但隨後它將卷添加到相同的ArrayList中,這讓我認爲我意外地引用了同一個對象,但是您可以看到我將對象ID打印到控制檯,並顯示t我確實稱之爲PlayerScore對象的不同參考,它應該是[5,5]爲我的得分,然後[5,5]爲第1幀中的derps得分,任何幫助表示讚賞
+1的提示,但最後但並非最不重要也爲你看似無限的耐心,而對沒有從最初發布的代碼 – Marco13
@ Marco13運行害怕:謝謝。 –
是的,我爲這個潦草的代碼表示歉意,我只是從日食中粘貼它。下一次我將格式化時,這些不必要的ArrayLists也被用在早期版本的代碼中,我忘記刪除它們。感謝關於掃描器的提示,最後,@ Marco13再次感謝您花時間閱讀我的噁心代碼 –