【问题标题】:Unable to end this dice game properly无法正确结束此骰子游戏
【发布时间】:2021-12-07 08:13:47
【问题描述】:

我正在尝试正确结束此游戏,但 do while 循环或我正在执行的操作导致游戏继续进行。如果有人有进一步的建议让我在任一得分达到 99 后结束这场比赛,我将不胜感激。我已经粘贴了这两个类的来源。我也是学生,请见谅。

package programming2;

import java.util.Random;
import java.util.Scanner;

public class Matador {

    public static void main(String[] args) {
        
        /*The game is played between the player and the computer.
        The player's score and the computer's score each begin at 0.
        The object of the game is to be the first reach 99 points.
        The player and the computer take turns. On each turn: */
        
        Scanner scnr = new Scanner(System.in);  
        Die matador = new Die();
        
        int playerScore = 0;
        int computerScore = 0;
        int playerPick = 0;
        int cpu;
        int turn;
        
        Random randGen = new Random();
        cpu = randGen.nextInt(6) + 2; 
        int cpuPick = 0;
        int quit = 0;
        
        System.out.println("Welcome to el Matador!");
        System.out.println("The winner of this game is the first to score 99 points");
        
        do {System.out.println("Player 1 please pick your number");  // start of master loop
        
        playerPick = scnr.nextInt();
        
        System.out.println("Player 1 you selected " + playerPick);
        
        System.out.println("Player 2 please pick your number");
        
        cpuPick = cpu;
        
        System.out.println("Player 2 selects " + cpuPick);
        
        
           // 
        do {    // start of game loop - master do loop player 1
            
            if (computerScore > 98) {
                
                quit = 1;
                break;
            }
            
            System.out.println("Player 1 rolls dice");
            
            matador.rollDieNow();
            
            System.out.println("Values for 1st dice is: " + matador.getValueDie1() + " and " + matador.getValueDie2());
            System.out.println("Combined values are " + matador.getValue());
            
            turn = 0;       // start of loop marker for player 1 
            
            
            
            if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) {
        
            
            playerScore = playerScore + -25;
            break;
            
            }
            
            
        
        if (matador.getValueDie1() == 1 || matador.getValueDie2() == 1) {
            
            playerScore = playerScore + 0;
            System.out.println("Changing Turns!");
            break;
            
        }
        
        if (matador.getValueDie1() != 1 || matador.getValueDie2() != 1) {     // sum of dicevalue added to score 
            
            playerScore = playerScore + matador.getValue();
           
           
           }
        
        if (playerPick == matador.getValueDie1() || playerPick == matador.getValueDie2()) {  // if picked number appears on at least 1 
            
            turn = turn + 1;
        }
        
        if (playerPick == matador.getValueDie1() && playerPick == matador.getValueDie2()) {   // if picked number appears on both dice
            
            System.out.println("You are the winner player 1 you matched your numbers!!");
            System.out.print("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
            quit = 1;
            break;
            
            
        }
        
       
            
            System.out.println("Player 1 Score: " + playerScore);
        
        
        }
    
        while (turn < turn + 1 || quit != 1);
        
        // turn changes and also end of loop marker 
        System.out.println("Score update! Player 1 score is: " + playerScore + " |-| player 2 score is " + computerScore);
        
        turn = 0;     // turn counter resets 

        do {       // start of computer player's turn 
            
            if (playerScore > 98) {
                
            quit = 1;
            break;
            
            }
            
            System.out.println("Player 2 rolls dice");
            matador.rollDieNow();
            System.out.println("Values for 1st dice is: " + matador.getValueDie1() + " and " + matador.getValueDie2());
            System.out.println("Combined values are " + matador.getValue());
            
            
            
            if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) {    // loop marker for cpu starts 
    
        
        computerScore = computerScore + -25;   
        
    
        break;
        
        }
        

    
    if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) {      // loses turns if 1 appears on both sides
        
        computerScore = computerScore + 0;
        System.out.println("Changing Turns!");
        break;
        
    }
    
    if (matador.getValueDie1() != 1 || matador.getValueDie2() != 1) {  // computer scores  value of dice 
        
        computerScore = computerScore + matador.getValue();
        
        if (computerScore > 98) {
    
        break; 
        
        }
        
    }
    
    if (cpuPick == matador.getValueDie1() || cpuPick == matador.getValueDie2()) {
        
        turn = turn + 1;
    }
    
    if (cpuPick == matador.getValueDie1() && cpuPick == matador.getValueDie2()) {
        
        System.out.println("You are the winner player 2 you matched your numbers!!");
        System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
        quit = 1;
        break;
    }
        
    
    System.out.println("Player 2 Score: " + computerScore);
    
    
        }   

        
        
    while (playerScore < 98 || computerScore <98 || quit != 1);   
        
        
        
        }
        
        while (playerScore < 98 || computerScore < 98 || quit != 1);    // end of master loop 
        System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
        

        
        

    }

}

如果需要其他类,但它与分数无关,因为它主要用于确定一些值:

package programming2;

import java.util.Random;
import java.util.Scanner;

public class Die {   
    
    private int die1;   //instance variable 
    private int die2;
    private int rolledDice;
    
    
    public Die() {                //constructor assigns random value
        
        
        
        die1 = (int) (Math.random()*6)+1;   
        die2 = (int) (Math.random()*6)+1;
          
                
    }
    
    public void rollDieNow () {                     //rolls dice 
        
        
        Random randGen = new Random();
        
        
        
        die1 = randGen.nextInt(6) + 1; 
        die2 = randGen.nextInt(6) + 1; 
           
       }
    
     
    public int getValue() {       // gets dice value 
    
    
        
    rolledDice = die1 + die2;
    
        
    return rolledDice;
    
    }
    
    public int getValueDie1() {       // gets die1 value 
        
        
        
        
        
            
        return die1;
        
        }
    
    public int getValueDie2() {       // gets die2 value 
        
        
        
        
        
            
        return die2;
        
        }

    

}

【问题讨论】:

  • 这个条件是什么意思:turn &lt; turn + 1?
  • while (playerScore &lt; 98 || computerScore &lt; 98 || quit != 1) || 应该是&amp;&amp;。只要满足所有这些条件,而不是至少满足其中一个条件,您就希望运行循环。目前,您的循环只会在 playerScore &gt;= 98computerScore &gt;=98quit==1 时停止。但只要达到其中一个条件就应该标志着游戏的结束。
  • 我使用变量 turn 来确定玩家何时结束,如您所见的 if 条件。在某些情况下回合增加回合丢失,所以我使用 break 来打破循环并让其他玩家选择。

标签: java dice


【解决方案1】:

骰子类

package finalproject2;

import java.util.Random;

public class Die {

    Random randGen = new Random();
    private int value; // instance variable -> stores the roll
    // each Die object gets its own copy of the instance variable
    
    public Die() { // constructor rolls dice (calls rollDieNow() method)
        rollDieNow();
    }

    public void rollDieNow() { // rolls dice
        value = randGen.nextInt(6) + 1;
    }

    public int getValue() { // gets dice value
        return value;
    }

}

斗牛士班

package finalproject2;


import java.util.Random;
import java.util.Scanner;

public class Matador {

    public static void main(String[] args) {
        
        /*The game is played between the player and the computer.
        The player's score and the computer's score each begin at 0.
        The object of the game is to be the first reach 99 points.
        The player and the computer take turns. On each turn: */
        
        Scanner scnr = new Scanner(System.in);  
        
        Die d1 = new Die();
        Die d2 = new Die();
        
        
        
        int playerScore = 0;
        int computerScore = 0;
        int playerPick = 0;
        int cpu;
        int turn;
        int sum ;
        
        Random randGen = new Random();
        cpu = randGen.nextInt(6) + 2; 
        int cpuPick = 0;
        int quit = 0;
        
        System.out.println("Welcome to el Matador!");
        System.out.println("The winner of this game is the first to score 99 points");
        
        // use System.exit(0) to stop program execution
        
        do {
            
            if (playerScore >= 99 || computerScore >= 99) {
                
            System.out.println("Game Over!");
            System.exit(0);
            }
            
            System.out.println("Player 1 please pick your number");  // start of master loop
        
        playerPick = scnr.nextInt();
        
        System.out.println("Player 1 you selected " + playerPick);
        
        System.out.println("Player 2 please pick your number");
        
        cpuPick = cpu;
        
        System.out.println("Player 2 selects " + cpuPick);
        
        
           // 
        do {    // start of game loop - master do loop player 1
            
        
            System.out.println("Player 1 rolls dice");
            
            d1.rollDieNow();
            d2.rollDieNow();
            sum = d1.getValue() + d2.getValue() ;
            
            System.out.println("Values for both dice are: " + d1.getValue() + " and " + d2.getValue());
            System.out.println("Combined values are " + sum);
            
            turn = 0;       // start of loop marker for player 1 
            
            
            
            if (d1.getValue() == 1 && d2.getValue() == 1) {
        
            
            playerScore = playerScore + -25;
            System.out.println("Ouch! Lost 25 points!");

            break;  // lost of turn, loop ends 
            
            }
            
            
        
        if (d1.getValue() == 1 || d2.getValue() == 1) {
            
            playerScore = playerScore + 0;
            System.out.println("Changing Turns!");
            break; // lost of turn loop ends
            
        }
        
        if (d1.getValue() != 1 || d2.getValue() != 1) {     // sum of dice value added to score 
            
            playerScore = playerScore + (d1.getValue() + d2.getValue());
            System.out.println("Gained points! " + (d1.getValue() + d2.getValue()));

           
           
           }
        
        if (playerPick == d1.getValue() || playerPick == d2.getValue()) {  // if picked number appears on at least 1 
            
            turn = turn + 1;
        }
        
        if (playerPick == d1.getValue() && playerPick == d2.getValue()) {   // if picked number appears on both dice
            
            System.out.println("You are the winner player 1 you matched your numbers!!");
            System.out.print("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
            System.exit(0); // game ends completely
            
            
        }
        
       
            
            System.out.println("Player 1 Score: " + playerScore);
        
        
        }
    
        while (turn < 1);
        
        // turn changes and also end of loop marker 
        System.out.println("Score update! Player 1 score is: " + playerScore + " |-| player 2 score is " + computerScore);
        
        turn = 0;     // turn counter resets 

        do {       // start of computer player's turn 
            
            if (playerScore >= 99 || computerScore >= 99) {
                
                System.out.println("Game Over!");
                System.exit(0);
                }
            
            System.out.println("Player 2 rolls dice");
            d1.rollDieNow();
            d2.rollDieNow();
            System.out.println("Values for 1st dice is: " + d1.getValue() + " and " + d2.getValue());
            System.out.println("Combined values are " + (d1.getValue() + d2.getValue()));
            
            
            
            
            if (d1.getValue() == 1 && d2.getValue() == 1) {    // loses points and turn  
    
        
        computerScore = computerScore + -25; 
        System.out.println("Ouch! Lost 25 points!");
        
    
        break;
        
        }
        

    
    if (d1.getValue() == 1 || d2.getValue() == 1) {      // loses turns if 1 appears on both sides
        
        computerScore = computerScore + 0;
        System.out.println("Changing Turns!");
        break;
        
    }
    
    if (d1.getValue() != 1 || d2.getValue() != 1) {  // computer scores  value of dice 
        
        computerScore = computerScore + (d1.getValue() + d2.getValue());
        
        System.out.println("Gained points! " + (d1.getValue() + d2.getValue()));
        
        }
        
    
    
    if (cpuPick == d1.getValue() || cpuPick == d2.getValue()) {
        
        turn = turn + 1;
    }
    
    if (cpuPick == d1.getValue() && cpuPick == d2.getValue()) {
        
        System.out.println("You are the winner player 2 you matched your numbers!!");
        System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
        System.exit(0);
    }
        
    
    System.out.println("Player 2 Score: " + computerScore);
    
    
        }   

        
        
    while (turn < 1);   // for some reason on my previous revision this was set to 
                        // a different condition than player 1, now the game ends appropiate
        
        
        
        }
        
        while (playerScore <= 99 || computerScore <= 99);    // end of master loop 
        System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
        

        
        

    }

}

【讨论】:

  • 将代码转储到帖子中并不是真正的答案。你在干嘛?原版有什么问题?你的代码是如何解决这个问题的?
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 2015-08-25
相关资源
最近更新 更多