if / else语句和while循环

时间:2015-10-16 03:31:45

标签: java loops if-statement while-loop

我正在为学校分配工作而且我无法通过playerTurn方法,因为do while部分不会为我编译。这就是我写的:

public class Pig {
    public static void main (String[] args){
        Random generator = new Random();
        Scanner console = new Scanner(System.in);
        int roll = diceRoll(generator);
        System.out.println("You rolled a " + roll);


}
public static int diceRoll(Random generator){
    int num = generator.nextInt(6) + 1;
    return num;
}
public static int playerTurn(java.util.Scanner input, java.util.Random rand){
    int turnScore = 0;
    int roll;
    String response;

    do {
        roll = diceRoll(generator);
        System.out.println("You rolled a " + roll);

        if ( roll == 1) {
            turnScore = 0;
            System.out.println("Oh no! You rolled a 1 which means your turn is over and your score is 0");
            return turnScore;
        } else {
            System.out.println("Do you want to roll again? Yes or No");
            response = console.next();
        } while (response.equalsIgnoreCase("Yes")) {
            return turnScore;
        }
    }
}   

}

我不断收到的编译错误是:

Pig.java:35:错误:正在预料中                 }                  ^

Pig.java:37:错误:非法开始表达 } ^

Pig.java:37:错误:解析时到达文件末尾 }  ^

Pig.java:40:错误:解析时到达文件末尾

4个错误

我非常确定我的所有括号都匹配,但如果不是这样,我就不知道如何修复它。提前致谢。

1 个答案:

答案 0 :(得分:0)

这里将为ya编译。

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

public class Pig
{
  public static void main (String[] args)
  {
    Random generator = new Random();
    Scanner console = new Scanner(System.in);
    int roll = diceRoll(generator);
    System.out.println("You rolled a " + roll);

  }
  public static int diceRoll(Random generator)
  {
     int num = generator.nextInt(6) + 1;
     return num;
  }

  public static int playerTurn(java.util.Scanner input, java.util.Random rand)
  {
      int turnScore = 0;
      int roll;
      String response;

      do 
      {
          roll = diceRoll(rand);
          System.out.println("You rolled a " + roll);

          if ( roll == 1) 
          {
              turnScore = 0;
              System.out.println("Oh no! You rolled a 1 which means your turn is over and your score is 0");
              return turnScore;
          } 
          else 
          {
              System.out.println("Do you want to roll again? Yes or No"); 
              response = input.next();
          } 
      }while (response.equalsIgnoreCase("Yes"));
     return turnScore;
 }
}