好的,所以我有一个保龄球游戏得分计算器,其中我有主要类从命令行读取输入并为每个玩家创建一个“PlayerScore”对象,PlayerScore对象有一个arraylist和一个添加方法对于那个arraylist,游戏应该逐帧进行并在玩家之间交替,询问他们在每个帧中击倒的引脚数量,然后计算并生成格式化的分数表,所以例如我输入将有两个玩家,然后我将玩家名称存储在一个String ArrayList中,然后将一个空的playercore对象存储在一个单独的相应的PlayerScore Arraylist中,我有一个for循环,它在嵌套在for循环中的玩家之间交替,跟踪当前帧,所以当我输入一个玩家的分数时,它应该引用他们的playercore对象,这是我的代码:
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
PlayerScore@169ca65
[5, 5]
Frame : 1
enter rolls for derp
roll 1 :
5
roll 2 :
5
PlayerScore@1301ed8
[5, 5, 5, 5]
Frame : 2
enter rolls for matt
roll 1 :
好的,你可以看到它将roll [5,5]添加到matt的playscore对象中的arraylist但是然后它将滚动添加到同一个ArrayList,这让我觉得我不小心引用了同一个对象但是as你可以看到我打印出对象ID到控制台,它显示我确实调用了PlayerScore对象的不同引用,对于我的分数应该是[5,5]然后[5,5]对于第1帧中的derps得分,任何帮助表示赞赏
答案 0 :(得分:3)
答案 1 :(得分:0)
您已在PlayerScore类中将得分变量声明为静态,这意味着它是一个类变量 - 只有一个将由您创建的两个PlayerScore对象共享。
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html