添加到对象字段

时间:2016-04-21 23:55:42

标签: java object

目标是通过控制台创建一个带有文本菜单的双人骰子投掷游戏。我有3个课程,游戏,玩家和骰子。玩家类只有字符串玩家名字段和int玩家得分字段。骰子类有RNG代码,但大部分游戏都在游戏类中。

在开始游戏时,我命名两个玩家(从用户处拍摄的文字)并创建两个玩家对象。我可以调用“滚动骰子”方法从一轮游戏中返回得分,但是如何将该得分添加到已经在玩家对象中定义的字段?

每个玩家应该有一个对象,但我似乎无法引用每个单独的对象。是否存在使用不同方法创建这些对象但是与尝试增加玩家得分的方法具有相同类别的问题?

    import java.util.Scanner;

    public class Game
    {
        // instance variables - replace the example below with your own
        private int x;

        /**
         * Constructor for objects of class Game
         */
        public Game()

        {
            // initialise instance variables
            x = 0;

        }

        public void menu()
        {

        Scanner scanner = new Scanner(System.in);


           System.out.println("Welcome to My Dice-and-Roll Game!");
            System.out.println("=================================");
            System.out.println("(1) Start a New Game");
            System.out.println("(2) Play One Round");
            System.out.println("(3) Who is leading now?");
            System.out.println("(4) Display Game Help");       
            System.out.println("(5) Exit Game");        
            System.out.println("(6) Choose an option:");       

        int selection = scanner.nextInt();

        switch (selection) {
            case 1:
                // Perform "original number" case.
                        nameplayers();
                        playround();
                break;
            case 2:
                // Perform "encrypt number" case.
                        System.out.println("(2) ");
                break;
            case 3:
                // Perform "decrypt number" case.
                                    System.out.println("(3) ");
                break;
            case 4:
                // Perform "quit" case.
                                    System.out.println("(4) ");
                break;
            default:
                // The user input an unexpected choice.



        }


    }

    public void nameplayers()
    {
    String namep1;
    String namep2;
    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter the name of player 1: ");
    namep1 = scanner.next( );
    Player P1 = new Player();
    P1.setName(namep1);
    System.out.print("Enter the name of player 2: ");
    namep2 = scanner.next( );
    Player P2 = new Player();
    P2.setName(namep2);


    }

        public void rolldice()
        {
            int dice1;
            int dice2;
            int rollTotal;

            // put your code here
          dice1 = (int)(Math.random()*6) + 1;
          dice2 = (int)(Math.random()*6) + 1;
          if(dice1 == dice2){
              rollTotal = ((dice1 + dice2) *2);
            }
             else {
                 rollTotal = (dice1 + dice2);
                }



          System.out.println(" has rolled a " + dice1 + " and " + dice2 + " which totals " + rollTotal + " points");
          P1.addScore(rollTotal);

    }

    public void playround()
    {
    String namep1;
    String namep2;
    Scanner scanner = new Scanner(System.in);

    System.out.println("The dice are thrown!");
    try{
        Thread.sleep(2000);//2000ms = 2s
    }catch(InterruptedException ex){
    }
    System.out.println("..");
    try{
        Thread.sleep(2000);//2000ms = 2s
    }catch(InterruptedException ex){
    }
    System.out.println(" ..");
    try{
        Thread.sleep(2000);//2000ms = 2s
    }catch(InterruptedException ex){
    }
    System.out.println("   :");
    try{
        Thread.sleep(2000);//2000ms = 2s
    }catch(InterruptedException ex){
    }
    System.out.println("    ..");
    try{
        Thread.sleep(2000);//2000ms = 2s
    }catch(InterruptedException ex){
    }


    rolldice();

    }
    }

        import java.util.Scanner;


public class Player
{
    // the name of the player
    private String playerName;

     // the current score of the player
    private int playerScore;

    /**
     * Constructor for objects of class Player
     */
    public Player()
    {

    String playerName = "";

    int playerScore = 0;

    }


    public void setName(String newName)
    {
        // put your code here
        playerName = newName;
                    System.out.println("Player name set as " +
                               newName);

    }
    public void addScore(int addScore)
    {
        // put your code here
        playerScore = playerScore + addScore;

    }

}

2 个答案:

答案 0 :(得分:0)

一旦离开方法(方法范围),对象不再可见/引用,您需要引用Players' outside of the method. Because you declare Player player inside the namePlayers()`方法,并且变得可以被垃圾收集。

将玩家定义为Game类的本地变量:

public class Game
{
    Player player1;
    Player player2;
}

然后在你的namePlayers()方法中,创建并设置玩家:

P1 = new Player();
P1.setName(namep1);
System.out.print("Enter the name of player 2: ");
namep2 = scanner.next( );
P2 = new Player();
P2.setName(namep2);

答案 1 :(得分:0)

最面向对象的方法是编写一个更新私有变量的方法。类似的东西:

public void updateScore(int score) {
    this.x = x + score;
{

您可以在播放器对象上调用此方法,例如:     player1.updateScore(resultPlayer1);     player2.updateScore(resultPlayer2);