【问题标题】:If/else statements and do while loopsif/else 语句和 do while 循环
【发布时间】:2015-10-16 03:31:44
【问题描述】:

我正在为学校做作业,但我无法通过 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 个错误

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

【问题讨论】:

  • 你的牙套错了。 while 条件在结束 } 之后,它在第 30 行关闭 else 语句,而它应该在第 33 行结束 } 之后。
  • A do/while 语句不应该有括号......只是 while(condition); 只是暂时这样
  • 还有,你第31行的return turnScore;放错地方了,不能放在while语句中,需要放在方法的最后,
  • 一个好的代码编辑器,如 Sublime Text 或 Nitrous.io 在匹配开始和结束大括号时会有所帮助。当您的光标靠近 { 或 } 时,匹配的大括号会发光。

标签: java loops if-statement while-loop


【解决方案1】:

这里将为你编译。

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;
 }
}

【讨论】:

  • 请将while 放在与结束大括号相同的行上,否则它看起来像一个空主体的新while 循环。
猜你喜欢
  • 2012-06-18
  • 2019-04-24
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 2015-04-07
  • 2014-07-01
  • 1970-01-01
相关资源
最近更新 更多