首页 > 解决方案 > 编写一个程序,在计算机和用户之间玩一个简单的骰子游戏。当程序运行时,一个循环应该重复 10 次

问题描述

我无法找到我在此程序中寻找的结果

这是我正在寻找的结果:

Roll 1:
User got: 6. Computer got: 3. User wins.
Roll 2:
User got: 6. Computer got: 2. User wins.
Roll 3:
User got: 6. Computer got: 1. User wins.
Roll 4:
User got: 5. Computer got: 5. Its a tie.
Roll 5:
User got: 1. Computer got: 1. Its a tie.
Roll 6:
User got: 6. Computer got: 6. Its a tie. 

但这是我得到的结果:

Computer won turn1
Computer was the grand winner, winningnullout of10turns
Computer won turn2
Computer was the grand winner, winningnullout of10turns
Computer won turn3
Computer was the grand winner, winningnullout of10turns
Computer won turn4
Computer was the grand winner, winningnullout of10turns
Computer won turn5
Computer was the grand winner, winningnullout of10turns
Computer won turn6
Computer was the grand winner, winningnullout of10turns
Computer won turn7
Computer was the grand winner, winningnullout of10turns
Computer won turn8
Computer was the grand winner, winningnullout of10turns
Computer won turn9
Computer was the grand winner, winningnullout of10turns
Computer won turn10
Computer was the grand winner, winningnullout of10turns

这是我的代码:

package HW4;

import java.util.Random;


public class DiceGame {

    
    

    public static void main(String[] args ) {
        // TODO Auto-generated method stub
        
        Random random = new Random();
        int totalNumberOfTurns = 10;
        int computerDie;
        int computerScore = 0;
        int userScore = 0;
    
        for( int turn = 1; turn <= totalNumberOfTurns; turn++) {
            computerDie = random.nextInt( 6 ) + 1;
            
            
            int userDie1 = 0;
            
            
            if ( computerDie > userDie1 ) {
            computerScore = computerScore + 1;
                System.out.println("Computer won turn" + turn );
            } else if ( userDie1 > computerDie ) {
                System.out.println("User won turn" + turn );
                userScore = userScore + 1;
            } else { 
                System.out.println("turn" + turn + "was a tie" );
                
                
                
            }
            
            
            
            
        
            Object ComputerScore = null;
            Object userScore1 = null;
            if ( computerDie > userDie1 ) {
                System.out.println("Computer was the grand winner, winning" + ComputerScore + "out of" + totalNumberOfTurns + "turns" );
            
            
            } else if ( userDie1 > computerDie ) {
                System.out.println("User was the grand winner, winning" + ComputerScore + "out of" + totalNumberOfTurns + "turns" );
                
            } else if(ComputerScore == userScore1) { 
                System.out.println("It was a tie with computer winning" + ComputerScore + "turns and user winning" + userScore + "turns all out of" + totalNumberOfTurns ); 
            
            
        }
        
        
        }
    }
}
        

标签: java

解决方案


我不确定我是否正确理解了您的问题,但如果问题是每个回合后都会显示“大赢家”,那是因为您将其包含在for循环中。您的代码的另一件事是您不接受任何输入到userDie1. 它总是保持打开状态0,因此computerDie价值总是在获胜,因为它更大。
我也不明白你为什么要创建这两个对象:

    Object ComputerScore = null;
    Object userScore1 = null;

您可能想要打印实际分数(userScore 和 compuierScore)而不是某些null对象。


推荐阅读