[英]Java Bowling Game - Referencing different objects
OK, so I have a bowling game score calculator and in it I have the main class that reads input from the command line and create a "PlayerScore" object for each player, the PlayerScore object has an arraylist and a method to add to that arraylist, the game is supposed to go frame by frame and alternate between the players, asking for the amount of pins they knocked down in each frame and then calculating and generating a formatted score sheet, So for example I input that there will be two players, then I store the player names in a String ArrayList and then an empty playerscore object in a seperate cooresponding PlayerScore Arraylist, I have a for loop that alternates between the players nested in a for loop that keeps track of the current frame, So when I input the scores for one player it should reference their playerscore object, here is my code: 好的,所以我有一个保龄球比赛得分计算器,并且其中有一个主类,它从命令行读取输入并为每个玩家创建一个“ PlayerScore”对象,PlayerScore对象具有一个arraylist和一个添加到该arraylist的方法。 ,游戏应该逐帧进行,并在玩家之间交替,要求他们在每一帧中击倒的针脚数量,然后计算并生成格式化的计分表,例如,我输入有两个玩家,然后将播放器名称存储在String ArrayList中,然后将空的playerscore对象存储在单独的对应PlayerScore Arraylist中,我有一个for循环,在嵌套在for循环中的播放器之间交替播放,以跟踪当前帧,所以当我输入时一个球员的得分应该参考他们的playerscore对象,这是我的代码:
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);
}
}
}
And here is the PlayerScore class 这是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;
}
now here is the output from a trial run 现在这是试运行的结果
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 :
Ok, so as you can see it adds the rolls [5,5] to the arraylist in matt's playscore object but then it adds the rolls to the same ArrayList which made me think that I was accidentally referencing the same object but as you can see I printed out the object ID's to the console and it shows that I am indeed calling different references of the PlayerScore object, it should be [5,5] for my score then [5,5] for derps score in frame 1, any help is appreciated 好的,如您所见,它会将卷[5,5]添加到matt的playcore对象的arraylist中,但随后将这些卷添加到相同的ArrayList中,这使我认为我不小心引用了同一对象,但是如您所见我在控制台上打印出了对象ID,它表明我确实在调用PlayerScore对象的不同引用,在第1帧中对我的分数应该为[5,5],对于derps分数应该为[5,5],这有帮助被赞赏
You have declared the score variable as static in the PlayerScore class which means it is a class variable - there is only one which will be shared by the two PlayerScore objects you have created. 您已经在PlayerScore类中将score变量声明为static,这意味着它是一个类变量-您创建的两个PlayerScore对象将共享一个变量。
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.