如何获取if语句以从Java中的特定代码行重新启动?

时间:2018-10-17 02:45:28

标签: java loops if-statement do-while

我是一名学习Java的学生,我正在寻找让玩家2再次掷骰的方法(因为如果玩家掷双倍,他们又可以掷骰),但是,如果我使用continue,它将跳回到do while语句的顶部。任何想法,将不胜感激! 这是代码:

/A2.java
/to simulate a double dice game between 2 players; the first one to reach a  
score of 75 wins

public class A2
{
public static void main (String[]args)
{
{
  //intitialize variables
  int playerOneScore = 0;
  int playerTwoScore = 0;
  int win = 75;
  do {
    //PLAYER 1
    int diceOne = (int)(Math.random()*6)+1;
    int diceTwo = (int)(Math.random()*6)+1;
    int sum = diceOne+diceTwo;
    playerOneScore=playerOneScore+sum;
    if (diceOne==diceTwo)
    {
      System.out.println ("player 1 rolled a "+diceOne+" and a "+diceTwo);
      System.out.println ("player 1 now has a score of : "+playerOneScore);
      //to break the loop immediatly if one player wins
      if (playerOneScore>win)
      {
        break;
      }
      System.out.println ("player 1 now gets to roll again");
      sum=0;
      continue;
    }
    else if (diceOne!=diceTwo)
    {
      System.out.println ("player 1 rolled a "+diceOne+" and a "+diceTwo);
      System.out.println ("player 1 now has a score of : "+playerOneScore);
      sum=0;
    }
    //to break the loop immediatly if one player wins
    if (playerOneScore>win)
    {
      break;
    }
    //PLAYER 2
    diceOne = (int)(Math.random()*6)+1;
    diceTwo = (int)(Math.random()*6)+1;
    sum = diceOne+diceTwo;
    playerTwoScore=playerTwoScore+sum;
    if (diceOne==diceTwo)
    {
      System.out.println ("player 2 rolled a "+diceOne+" and a "+diceTwo);
      System.out.println ("player 2 now has a score of : "+playerTwoScore);
      //to break the loop immediatly if one player wins
      if (playerOneScore>win)
      {
        break;
      }
      System.out.println ("player 2 now gets to roll again");
      sum=0;
 //**********************THIS IS WHERE MY PROBLEM IS***********************
      continue;
    }
    else if (diceOne!=diceTwo)
    {
      System.out.println ("player 2 rolled a "+diceOne+" and a "+diceTwo);
      System.out.println ("player 2 now has a score of : "+playerTwoScore);
      sum=0;
    }
    //to break the loop immediatly if one player wins
    if (playerOneScore>win)
    {
      break;
    }
  }while (playerOneScore<=win && playerTwoScore<=win );
  if (playerOneScore>win)
  {
    System.out.println ("player 1 wins with a score of : "+playerOneScore);
  }
  else if (playerTwoScore>win)
  {
    System.out.println ("player 2 wins with a score of : "+playerTwoScore);
   }
  }
 }
}

对不起,格式糟糕

编辑:现在可以使用,谢谢吓死温伯特对我的帮助! 以下是在其他人也有类似问题的情况下的工作代码:

//A2.java
//to simulate a double dice game between 2 players; the first one to reach a 
score of 75 wins

public class A2
{
  public static void main (String[]args)
{
 {
  //intitialize variables
  int playerOneScore = 0;
  int playerTwoScore = 0;
  int win = 75;
  do {
    //PLAYER 1
    int diceOne = (int)(Math.random()*6)+1;
    int diceTwo = (int)(Math.random()*6)+1;
    int sum = diceOne+diceTwo;
    playerOneScore=playerOneScore+sum;
    if (diceOne==diceTwo)
    {
      System.out.println ("player 1 rolled a "+diceOne+" and a "+diceTwo);
      System.out.println ("player 1 now has a score of : "+playerOneScore);
      //to break the loop immediatly if one player wins
      if (playerOneScore>win)
      {
        break;
      }
      System.out.println ("player 1 now gets to roll again");
      sum=0;
      continue;
    }
    else if (diceOne!=diceTwo)
    {
      System.out.println ("player 1 rolled a "+diceOne+" and a "+diceTwo);
      System.out.println ("player 1 now has a score of : "+playerOneScore);
      sum=0;
    }
    //to break the loop immediatly if one player wins
    if (playerOneScore>win)
    {
      break;
    }
    //PLAYER 2
    boolean t=true;
    while (t){
      diceOne = (int)(Math.random()*6)+1;
      diceTwo = (int)(Math.random()*6)+1;
      sum = diceOne+diceTwo;
      playerTwoScore=playerTwoScore+sum;
      if (diceOne==diceTwo)
      {
        System.out.println ("player 2 rolled a "+diceOne+" and a "+diceTwo);
        System.out.println ("player 2 now has a score of : 
"+playerTwoScore);
        //to break the loop immediatly if one player wins
        if (playerOneScore>win)
        {
          break;
        }
        System.out.println ("player 2 now gets to roll again");
        sum=0;
        //NEED TO FIX, KEEPS JUMPING TO BEGINING AGAIN
        continue;
      }
      else if (diceOne!=diceTwo)
      {
        System.out.println ("player 2 rolled a "+diceOne+" and a "+diceTwo);
        System.out.println ("player 2 now has a score of : 
"+playerTwoScore);
        sum=0;
      }
      //to break the loop immediatly if one player wins
      if (playerOneScore>win)
      {
        break;
      }
      break;
    }
  }while (playerOneScore<=win && playerTwoScore<=win );
  if (playerOneScore>win)
  {
    System.out.println ("player 1 wins with a score of : "+playerOneScore);
  }
  else if (playerTwoScore>win)
  {
    System.out.println ("player 2 wins with a score of : "+playerTwoScore);
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

这里提供的示例说明了为什么通常人们不会在一个地方编写所有代码。

您的游戏有点复杂,这种将所有内容放入main中的方法可能效果不佳(尽管自从我开始编写此答案后,我发现您已经找到了解决方案)。您的代码有些难以理解,我也不是专家,但是我至少可以看到一个问题。

例如,在您的特定问题中,可能会有rollDice(int playerNumber)风格的功能,您可以随时调用它,例如玩家必须重新滚动时。您可能已经注意到自己,基本上是为每个玩家重写了代码:很好,您只有两个玩家,假设您有十个玩家!

您可以通过多种方式来抽象此问题。在我看来,如果要启动(或重新启动,正如您提到的那样),则应该检查一些概念(例如什么是函数),然后再检查object-oriented programming,因为Java都是关于面向对象编程的