如何使用for循环计算变量?

时间:2016-12-17 11:08:46

标签: java loops for-loop

我正在设计一个模拟骰子游戏的程序。代码计算每轮获得的总分数以及每轮获胜的玩家。

我想获得总胜数和总积分。我尝试在主类中使用for循环,但我不确定如何在这个问题中实现它。

  

第1轮:

     

球员1 3 1 5分:9

     

球员2 2 6 6分:14

     

获胜者是玩家2

     

第2轮:

     

球员1 3 6 5分:14

     

球员2 2 3 2分:7

     

获胜者是玩家1

     

第3轮:

     

球员1 3 3 6分:12

     

球员2 5 4 6分:15

     

获胜者是玩家2

  

总胜场:玩家1 - > 1 /玩家2 - > 2

     

总分:玩家1 - > 35 /玩家2 - > 36

主要课程

import java.util.Scanner;

public class Game {
  // ------------------- FIELDS ------------------------    
        // Create instance of Scanner class
        public static Scanner input = new Scanner(System.in);
        // variables
        public static ThreeDiceScorer thrdiesc;

        public static int diceArray [];

    // ------------------ METHODS ------------------------  
        public static void main(String[] args) {

        int rounds; // input by user
        int players;  // input by user

        System.out.print("Please input number of rounds (grater or equal than 0) --> ");
        rounds = input.nextInt();
        System.out.print("\n");

        System.out.print("Please input number of players (grater or equal than 2) --> ");
        players = input.nextInt();
        System.out.print("\n");


         for (int r = 0; r < rounds; r++) { // loop for number of rounds
        int max = 0;
        int max_p = 0;
        System.out.println("Round " + (r+1) + ": ");
        for (int p = 0; p < players; p++) { //loop for players
            int diceArray[] = new int[3];
        for (int i = 0; i < diceArray.length; i++) { // loop for dice Array (data of Array)
        diceArray [i] = 1 + (int)(6 * Math.random());   
        }
        // Create new ThreeDice and calculator instances
        thrdiesc = new ThreeDiceScorer(diceArray [0], diceArray [1], diceArray [2]);

        //Calculate
        thrdiesc.calcTotalPoints();
        thrdiesc.printResult(p, r);
            if (thrdiesc.total > max) {
                max = thrdiesc.total;
                max_p = p;
            }
            }
         System.out.println("Winner is player " + (max_p + 1) + "\n");
            }
        System.out.println("Total wins: " );
        System.out.println("Total points: " );

    }//end Main Method  
} // end Class

计算类

public class ThreeDiceScorer {
     public static int total;
     public int die1;
     public int die2;
     public int die3;

     public ThreeDiceScorer(int s1, int s2, int s3) {
          die1 = s1;
          die2 = s2;
          die3 = s3;
     }
public void calcTotalPoints() {
    int sumOfDice = die1 + die2 + die3;
         total= sumOfDice;
    }

      public void printResult(int p, int r) {
        System.out.println("player " + (p + 1) + "   " + die1 + " " + die2 + " " + die3 + " " + "points: " + total);
    }
}

2 个答案:

答案 0 :(得分:0)

有很多方法可以解决这个问题,我会添加两个计数器,比如int count = 0并递增它们:每次有胜利时(例如:if(victory) { c++ }),另一个通过表达式count = count + points。 请记住在开头实现这些int,或者for循环会将count1和count2重置为0.

答案 1 :(得分:0)

  

我尝试在主课程中使用for循环,但我不确定如何在这个问题中实现它。

我想说,一次做一件事。测试您的实施,一旦您确认它正在运行,然后继续。实现目标的顺序步骤是:

<强>步骤

  1. 首先为 一个 播放器实施骰子滚动。 (可以用方法完成)
  2. 为第二个玩家的掷骰子调用上面实施的方法。
  3. 决定胜利者

  4. 正确实施步骤1-3后,将步骤1-3包含在循环中(对于此特定情况,最好为for-loop

    //Example:
    int numOfRounds = 3;  //can receive this from user input
    
    for(int x=0; x<numOfRounds; x++){
        rollDice(playerOne);
        rollDice(playerTwo);
        decideWinner(playerOne, playerTwo);
    }
    
  5. 测试步骤1-4后,工作正常。实施总分显示:

    //Example:
    int numOfRounds = 3;  //can receive this from user input
    for(int x=0; x<numOfRounds; x++){
        rollDice(playerOne);
        rollDice(playerTwo);
        decideWinner(playerOne, playerTwo);            
    }
    displayFinalScore();
    
  6. 总分和总胜利可以存储在一个非常简单的Player类中,如下所示:

    public class Player{
        private String name;
        private int totalScore;
        private int totalWins;
    }
    

    动态多人游戏

    我尽量让解决方案尽可能短而简单,以便您理解。但是如果你想让程序动态地接收n个玩家。相同的程序流程仍然适用。

    在第4步中,您可以这样做:

    int numOfPlayers = 2; //can receive this from user input
    
    ArrayList<Player> players = new ArrayList<Player>();
    for(int x=0; x<numOfPlayers; x++)
        numOfPlayers.add(new Player("Player " + (x+1)));
    
    for(int x=0; x<numOfRounds; x++){
        for(int y=0; y<players.size(); y++)  //iterate through all players
            rollDice(players.get(y));
        decideWinner(players);               //decide the winner from all the players
    }