简体   繁体   English

Java Bowling游戏-引用不同的对象

[英]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],这有帮助被赞赏

  • One of you're problem is that you're using static fields, in particular the PlayerScore class's score ArrayList. 其中一个问题是您使用的是静态字段,尤其是PlayerScore类的得分ArrayList。 This is a problem as the same ArrayList is shared by all PlayerScore instances, which I believe is causing your main bugs. 这是一个问题,因为所有PlayerScore实例都共享同一ArrayList,我认为这是造成您的主要错误的原因。 The solution is easy: Don't use static fields here, and when you do use them, do so sparingly and for specific purpose. 解决方案很容易:不要在这里使用静态字段,当您使用静态字段时,请出于特定目的而谨慎使用。
  • Another problem is that your code has unnecessary junk in it. 另一个问题是您的代码中包含不必要的垃圾。 Why do you declare two completely different newscore and running_score fields in two different classes? 为什么在两个不同的类中声明两个完全不同的newscore和running_score字段? You will want to eliminate one of these. 您将要消除其中之一。
  • Also you are creating too many Scanner objects where only one is needed. 另外,您在只需要一个的情况下创建了太多的Scanner对象。
  • Your code formatting, especially your indentation, is horrific. 您的代码格式,尤其是缩进,非常可怕。 Why is this important? 为什么这很重要? The better your code formatting, the easier it is to see what line of code belongs to what block of execution, the easier it is to tell what your code is doing, the easier it is for both you and us to debug it. 代码格式越好,越容易看到哪一行代码属于哪个执行块,就越容易告诉您代码在做什么,您和我们双方都越容易调试它。 Don't try to make your and our jobs harder by not taking care to format your code well. 不要通过不小心格式化好代码来使您和我们的工作更加困难。 A little bit of effort will go a long way towards this. 一点点的努力将大大有助于这一点。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM