预期<identifier> - 缩进?

时间:2015-06-10 20:08:22

标签: java

public class CoinFlip {
        public static void main(String[] args) {
            //Get a random number
            //Decide the outcome of the coin flip
            //Get a second random number
            //Decide the outcome of a second coin flip
            //Print out which flip had a higher score
            //Print out the maximum of the two flips
            double myNumber;

            myNumber = Math.random();
        int score;
        if (myNumber < .4) {
                System.out.println("heads");
                score = 5;
        }
        else if (myNumber <.8) {
                System.out.println("tails");
                score = 2;
        }
        else if (myNumber <.85) {
                System.out.println("derr");
                score = 20;
        }
        else {
                System.out.println("go 2 sleep");
                score = -3;
        }

        System.out.println("arrgh the score for der flip wus: " + score);

        }
        public static int max(int a, int b);
            int a = score;
            int b = secondScore;
                int maxScore = Math.max(score, secondScore);
                    System.out.println(score, secondScore);
        {

        }

我通过int a和int b得到预期的错误。是缩进吗?我不知道该怎么做。我试图在缩进和括号周围移动无济于事。我是新来的,我很难过。我觉得这是一个非常简单的问题。

2 个答案:

答案 0 :(得分:1)

缩进只是为了便于阅读。您有三个问题我可以看到:

  1. 整个班级没有关闭'}'
  2. 你的max()方法是一个定义,不应该有';'在它之后
  3. 将max方法中的开头“{”移到方法逻辑之上。
  4. 希望这有帮助。

答案 1 :(得分:0)

public class CoinFlip {
    public static void main(String[] args) {
        // Get a random number
        // Decide the outcome of the coin flip
        // Get a second random number
        // Decide the outcome of a second coin flip
        // Print out which flip had a higher score
        // Print out the maximum of the two flips
        double myNumber;

        myNumber = Math.random();
        int score;
        if (myNumber < .4) {
            System.out.println("heads");
            score = 5;
        } else if (myNumber < .8) {
            System.out.println("tails");
            score = 2;
        } else if (myNumber < .85) {
            System.out.println("derr");
            score = 20;
        } else {
            System.out.println("go 2 sleep");
            score = -3;
        }

        System.out.println("arrgh the score for der flip wus: " + score);

    }

    public static int max(int score, int secondScore) {
        int a = score;
        int b = secondScore;
        int maxScore = Math.max(a, b);
        System.out.println("Score 1 = " + score + " Score 1 = " + secondScore);
        return maxScore;

    }

对于您的问题,请参阅

的语法
  1. System.out.println
  2. 如何编写方法并返回值
  3. 在类中声明方法
  4. 一切顺利