简体   繁体   English

Java:尝试获取键盘输入并在 if/else 条件下对其进行评估,以将 boolean 值设置为 true 或 false

[英]Java: Trying to take keyboard input and evaluate it in an if/else condition to set a boolean value to true or false

I'm working on building out a sample dice rolling game that rolls the dice 10 times unless interrupted by user input.我正在开发一个示例掷骰子游戏,该游戏掷骰子 10 次,除非被用户输入打断。

I have a while statement that runs based on the state of two boolean values, one of which is set based on the user input.我有一个基于两个 boolean 值的 state 运行的 while 语句,其中一个是根据用户输入设置的。

My current progress is below.我目前的进度如下。 Any suggestions would be a huge help!任何建议都会有很大帮助!

import java.util.Scanner;
import java.swing.JOptionPane;
import java.util.Random;


public class DiceGame
{
   public static void main(String[] args)
   {


     Random rand = new Random();


      int player1Wins = 0;
      int player2Wins = 0;
      int ties = 0;
      int rollCount = 0;
      boolean rollAgain = true;

      while (rollCount < 10 && rollAgain)
      {
         int player1Dice = rand.nextInt(6) + 1;
         int player2Dice = rand.nextInt(6) + 1;

         if (player1Dice > player2Dice)
         {
            player1Wins++;
            System.out.println("Player 1 wins!!");
         } 

         else if (player2Dice > player1Dice)
         {
            player2Wins++;
            System.out.println("Player 2 wins!!");
         }

         else 
         {
            ties++;
            System.out.println("It's a tie...");
         }

         rollCount++;
         String answer;
         Scanner keyboard = new Scanner(System.in);
         System.out.println("Would you like to roll again? Press y for yes");
         answer = keyboard.nextLine();

         if (answer == "y") 
         {
            rollAgain = true;
         }

         else
         {
            rollAgain = false;
         }

      }
      System.out.println();
      System.out.println("Player 1 Total wins: " + player1Wins);
      System.out.println("Player 2 Total wins: " + player2Wins);
      System.out.println("Total Ties: " + ties);
      System.out.close();
   }
}

Try to break down the code based on the single responsability principle.尝试根据单一职责原则分解代码。 Clean code, easy to test, and much mmore granular.干净的代码,易于测试,更细粒度。

I did one quick implementation, far to be the best solution, but it is just to exemplify tsuch principle:我做了一个快速的实现,远不是最好的解决方案,但这只是为了举例说明这样的原则:

Dice object, but could be named as Player I think骰子 object,但我认为可以命名为 Player

public class Dice implements Comparable<Dice> {

    private int number;

    public Dice(int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

    @Override
    public int compareTo(Dice o) {
        return Integer.compare(number, o.number);
    }
}

Counter - check the rolls, show the winner and keep results计数器 - 检查卷,显示获胜者并保留结果

public class Counter {

    private int playerOneWins;
    private int playerTwoWins;
    private int ties;

    public void check(Dice player1, Dice player2) {

        if (player1.compareTo(player2) > 0) {
            System.out.println("Player 1 wins!!");
            playerOneWins++;
        } else if (player2.compareTo(player1) > 0) {
            System.out.println("Player 2 wins!!");
            playerTwoWins++;
        } else {
            System.out.println("It's a tie...");
            ties++;
        }
    }

    public void showResults() {
        System.out.println();
        System.out.println("Player 1 Total wins: " + playerOneWins);
        System.out.println("Player 2 Total wins: " + playerTwoWins);
        System.out.println("Total Ties: " + ties);
        System.out.close();
    }
}

Input Reader - know how to get inputs from user输入阅读器 - 知道如何从用户那里获取输入

public class InputReader {

    private static final String YES = "y";

    private Scanner keyboard = new Scanner(System.in);

    public boolean askUser() {

        System.out.println("Would you like to roll again? Press y for yes");

        String answer = keyboard.nextLine();

        return YES.equalsIgnoreCase(answer);
    }
}

Roller - create number / throw the dices滚子 - 创建数字/掷骰子

public class Roller {

    private Random random = new Random();

    public Dice roll()
    {
        return new Dice(random.nextInt(6) + 1);
    }
}

And, finnaly, this is the main class:最后,这是主要的 class:

public class Game {

    private static final int MAX_ROLLS = 10;

    private Roller roller = new Roller();
    private Counter counter = new Counter();
    private InputReader inputReader = new InputReader();

    public void start() {

        int rolls = 0;
        boolean continueRolling = true;

        while (rolls <= MAX_ROLLS && continueRolling) {

            Dice player1 = roller.roll();
            Dice player2 = roller.roll();

            counter.check(player1, player2);

            rolls++;

            continueRolling = inputReader.askUser();
        }

        counter.showResults();
    }

    public static void main(String[] args) {
        new Game().start();
    }
}

I hope it helps you to understand such principle.我希望它可以帮助您理解这样的原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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