简体   繁体   English

我怎样才能在只能运行一次的循环中制作一个开关盒?

[英]How can i make a switch case in a loop that can only run once?

Im working on an assignment and i need to make a dice game, in the game the user is supposed to be able to roll a specific dice only once per round (example dice 1 can only be rolled once) however i cannot figure out how to do that, this is what i have so far我正在做一项作业,我需要制作一个骰子游戏,在游戏中,用户应该每轮只能掷一次特定的骰子(例如骰子 1 只能掷一次)但是我不知道如何这样做,这就是我到目前为止所拥有的

    class Main {
public static void main(String[] args) {
  Scanner diceInput = new Scanner(System.in);
  String diceNumber = "";
  int dice1 = 0;
  int dice2 = 0;
  int dice3 = 0;
  int win = 0;
  int loss = 0;
  int sum = 0;
  int rolls = 3;
 
  //intro
  System.out.print("Welcome to the game 12. You must roll 1-3 dice and try to get the sum 12");
  // while loop that repeats until q is inputted or rolls are higher than 0
  while((!"q".equals(diceNumber)) || (rolls > 0)){
    System.out.print("\nEnter which dice you want to roll [1,2,3] (exit with q): ");
   diceNumber = diceInput.nextLine();
    sum = 0;
    //switch for specific inputs that will repeat until a valid imput is entered.
  switch(diceNumber){
    case "1":
      dice1 = (int)(Math.random()*6)+1;
      sum = dice1 + dice2 + dice3;
      rolls--;//decreases a roll when case is run
      break;
    case "2":
      dice2 = (int)(Math.random()*6)+1;
      sum = dice1 + dice2 + dice3;
      rolls--;
      break;
    case "3":
      dice3 = (int)(Math.random()*6)+1;
      sum = dice1 + dice2 + dice3;
      rolls--;
      break;
    case "q":
      System.out.println("Game Over!");
      System.exit(0);
  }
     if(sum > 12) {//adds a point to loss if score is over 12
     loss++;
   } else if(sum == 12){// point to win if score is 12
     win++;
   }
    System.out.println(dice1+" "+dice2+" "+dice3+" "+"sum "+sum+" #win "+win+" # loss "+loss);
    // displays next round message when sum = 12 or rolls = 0 and resets the value of dice
    if((rolls == 0) || (sum == 12)){
      dice1 = 0;
      dice2 = 0;
      dice3 = 0;
      System.out.println("Next Round");
      rolls = 3;

i would appreciate if the solution will be on the simpler side如果解决方案更简单,我将不胜感激

what you want is to check whether the dice have been rolled before you execute the code inside the switch case.你想要的是在执行 switch case 内的代码之前检查骰子是否已经掷过。

There are many ways check for that, for example with a dedicated boolean variable for each dice.有很多方法可以检查它,例如每个骰子都有一个专用的 boolean 变量。 But in your specific case, the dice variables are all 0 until they are rolled, so you can check for that instead.但在您的特定情况下,骰子变量在滚动之前都是 0,因此您可以检查它。

sth like this像这样的

switch(diceNumber){
    case "1":
        if(dice1!=0){
            //your code
        }
    case "2":
        if(dice2!=0){
            //your code
        }
    case "3":
        if(dice3!=0){
            //your code
        }

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

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