Java帮助。硬币代码

时间:2015-03-22 21:08:12

标签: java if-statement int boolean

我正在做一个代码,希望我接受这个代码,而不是存储为int,将其存储为布尔

public class Coin {

    private final int HEADS = 0;
    private final int TAILS = 1; 

    private int face;

    //------------------------------------------
    // Sets up the coin by flipping it initially
    //------------------------------------------
    public Coin () {
        flip(); 
    }

    //-------------------------------------------------
    // Flips the coin by randomly choosing a face value.
    //-------------------------------------------------
    public void flip() {
        face = (int) (Math.random() * 2); 
    }

    //-----------------------------------------------------
    //Returns true of the current face of the coin is heads
    //-----------------------------------------------------
    public boolean isHeads () {
        return (face == HEADS); 
    }

    //------------------------------------------------
    //Returns the current face of the coin as a string
    //------------------------------------------------
    public String toString() {
        String faceName;
        if (face == HEADS)
            faceName = "Heads";
        else
            faceName = "Tails"; 
        return faceName; 
    }
}

我有这个,但不知道要将底部的其他声明更改为什么。它说错了。

public class Coin2 {

    private final boolean HEADS = false; 
    private final boolean TAILS = true;

    private boolean face;

    //------------------------------------------
    // Sets up the coin by flipping it initially
    //------------------------------------------
    public Coin2 () {
        flip(); 
    }

    //-------------------------------------------------
    // Flips the coin by randomly choosing a face value.
    //-------------------------------------------------
    public void flip() {
        face = (boolean) (Math.random() < 2); 
    }

    //-----------------------------------------------------
    //Returns true of the current face of the coin is heads
    //-----------------------------------------------------
    public boolean isHeads () {
        return (face == false);
    }

    //-----------------------------------------------
    //Returns the current face of the coin as a string
    //------------------------------------------------
    public String toString() {
        String faceName;
        if (face == false);
            faceName = "Heads";
        else
            faceName = "Tails"; 
            return faceName; 
    }
}

2 个答案:

答案 0 :(得分:2)

看起来你的if语句不应该有分号

if (face == false);
        faceName = "Heads";
        else faceName = "Tails"; 
    return faceName; 

应该阅读

if (face == false)
    faceName = "Heads";
else 
    faceName = "Tails"; 
return faceName; 

(也为可读性而格式化)

答案 1 :(得分:0)

如果进行此更改,您将获得更有趣的翻转结果:

public void flip() {
    face = Math.random() < .5; 
}

因为random()的javadoc说:

  

返回带有正号的double值,大于或等于0.0且小于1.0。

不需要(boolean)演员,所以我把它拿出来了。

下一个是一个令人讨厌的错误:

if (face == false);
        faceName = "Heads";
        else faceName = "Tails"; 
    return faceName; 

if末尾的分号是如此难以发现和调试,大多数程序员都遵循风格指南来帮助防止它发生。它看起来像这样:

if (face == false) {
    faceName = "Heads";
}
else {
    faceName = "Tails"; 
}
return faceName; 

始终使用花括号,它应该阻止这个额外的分号噩梦再次发生。缩进也要小心。弄乱它会弄乱你的大脑。