/*
3、 用Java实现如下的骰子游戏:丢下两个骰子,若分值的总值为7点,则赢;否则输。提示:
1)首先定义Die类。
提示:Die类表示一个骰子有faceValue一个静态属性,有一个roll()方法getFaceValue()。
roll()方法使faceValue为1~6中的一个随机值。getFaceValue()是取出faceValue值2)然后定义DiceGame类。
提示:DiceGame类有die1、die2两个静态属性,有一个play()方法。play()方法返回一个布尔类型,true表示分值的总值为7点,否则为false。3)最后写了同个Test类,对上面定义的类进行测试。
提示:写出主类,main()方法中产生DiceGame对象,执行play()方法后显示出输赢*/import java.util.Random;
class Die
{
public void roll()
{
Random x = new Random();
faceValue = (int)(x.nextDouble()*6+1);
System.out.println(this.faceValue);
} public int getFaceValue()
{
return faceValue;
} private static int faceValue;
}class DiceGame
{
public boolean play(Die x,Die y)
{
if (die1.getFaceValue() + die2.getFaceValue() == 7)
{
return true;
//System.out.println("您赢了!");
}
else
//System.out.println("您输了!");
return false;
} private static Die die1;
private static Die die2;
};public class TestFace
{
public static void main (String [] args) //throws Exception
{
//try{
DiceGame dg = new DiceGame();
Die die1 = new Die();
Die die2 = new Die();
die1.roll();
die2.roll();
if (dg.play(die1,die2))
{
System.out.println("123");
}
System.out.println(die1.getFaceValue() + die2.getFaceValue());
/*}
catch (NullPointerException e)
{
System.out.println(e.getMessage());
}*/
}
};
自己刚做的一道题目,没弄懂为什么会出现NullPointerException,只要将code里面调用getFaceValue()获得faceValue改写成直接访问faceValue同时将faceValue的private去掉,就不会出现NullPointerException,但是结果似乎有些不对,希望有人能指出此程序里面存在的问题。

解决方案 »

  1.   

    1.问题解决了,我试着将faceValue的static去掉,并且修改play方法的传递参数和返回值将程序改成下面这样,不会出现
    NullPointerException异常,程序运行正常,问题应该出现在static上面。
    import java.util.Random;
    class Die
    {
    public void roll()
    {
    Random x = new Random();
    faceValue = (int)(x.nextDouble()*6+1);
    System.out.println(this.faceValue);
    } public int getFaceValue()
    {
    return faceValue;
    } private int faceValue;
    }class DiceGame
    {
    public void play(int x,int y)
    {
    if (x + y == 7)
    {
    System.out.println("您赢了!");
    }
    else
    System.out.println("您输了!");
    } private  Die die1;
    private  Die die2;
    };public class TestFace
    {
    public static void main (String [] args) //throws Exception
    {
    //try{
    DiceGame dg = new DiceGame();
    Die die1 = new Die();
    Die die2 = new Die();
    die1.roll();
    die2.roll();
    dg.play(die1.getFaceValue(),die2.getFaceValue());
    System.out.println(die1.getFaceValue() + die2.getFaceValue());
    /*if (dg.play(die1,die2))
    {
    System.out.println("您赢了!");
    }
    System.out.println("您输了!");
    System.out.println(die1.getFaceValue() + die2.getFaceValue());
    }
    catch (NullPointerException e)
    {
    System.out.println(e.getMessage());
    }*/ }
    }
    2.因为前面不符合题目对play方法的返回值的要求,于是我再次修改,但是只去掉faceValue的static,而不修改play方法的传递参数和返回值,同样会出现NullPointerException异常,貌似问题出现在传递参数的时候.....(继续思考)
    import java.util.Random;
    class Die
    {
    public void roll()
    {
    Random x = new Random();
    faceValue = (int)(x.nextDouble()*6+1);
    System.out.println(this.faceValue);
    } public int getFaceValue()
    {
    return faceValue;
    } private int faceValue;
    }class DiceGame
    {
    public boolean play(Die x,Die y)
    {
    if (die1.getFaceValue() + die2.getFaceValue() == 7)
    {
    return true;
    //System.out.println("您赢了!");
    }
    else
    return false;
    //System.out.println("您输了!");
    } private  Die die1;
    private  Die die2;
    };public class TestFace
    {
    public static void main (String [] args) //throws Exception
    {
    //try{
    DiceGame dg = new DiceGame();
    Die die1 = new Die();
    Die die2 = new Die();
    die1.roll();
    die2.roll();
    //dg.play(die1.getFaceValue(),die2.getFaceValue());
    //System.out.println(die1.getFaceValue() + die2.getFaceValue());
    if (dg.play(die1,die2))
    {
    System.out.println("您赢了!");
    }
    System.out.println("您输了!");
    System.out.println(die1.getFaceValue() + die2.getFaceValue());
    }
    /*catch (NullPointerException e)
    {
    System.out.println(e.getMessage());
    }*/ }
      

  2.   

    问题终于解决:import java.util.Random;
    class Die
    {
    public void roll()
    {
    Random x = new Random();
    faceValue = (int)(x.nextDouble()*6+1);
    System.out.println(this.faceValue);
    } public int getFaceValue()
    {
    return faceValue;
    } private int faceValue;
    }class DiceGame
    {
    public boolean play(Die x,Die y)
    {
    if (x.getFaceValue() + y.getFaceValue() == 7)
    {
    return true;
    //System.out.println("您赢了!");
    }
    else
    return false;
    //System.out.println("您输了!");
    } /*private  Die die1;
    private  Die die2;*/
    };public class TestFace
    {
    public static void main (String [] args) //throws Exception
    {
    //try{
    DiceGame dg = new DiceGame();
    Die die1 = new Die();
    Die die2 = new Die();
    die1.roll();
    die2.roll();
    //dg.play(die1.getFaceValue(),die2.getFaceValue());
    //System.out.println(die1.getFaceValue() + die2.getFaceValue());
    if (dg.play(die1,die2))
    {
    System.out.println("您赢了!");

    else
    System.out.println("您输了!");
    System.out.println(die1.getFaceValue() + die2.getFaceValue());
    }
    /*catch (NullPointerException e)
    {
    System.out.println(e.getMessage());
    }*/ }按照题目的意思我定义了在DiceGame里面定义了private Die die1;和private Die die2;问题就出在这里。在play(Die x,Die y)里面使用的是此类中定义的die1和die2,而这两个并没有被初始化,所以出现NullPointerExceptioe程序是总算有点眉目了,但是还是没有符合题目的要求:
    1.Die类表示一个骰子有faceValue一个静态属性.
    2.DiceGame类有die1、die2两个静态属性.
    再思考下,呵呵...看样子一个人在这里水了......
      

  3.   

    最终下个结论:题目出的有错误,无论如何faceValue不能为static的。应该static是共有的,改变的同时会改变所有对象中的faceValue!程序永远有错误!下面是最终得到的程序(除了faceValue不符合题目要求,其他全部符合题目要求了,如果有不对的地方,请高手指出来,Thanks!):import java.util.Random;
    class Die
    {
    public void roll()
    {
    Random x = new Random();
    faceValue = (int)(x.nextDouble()*6+1);
    System.out.println(this.faceValue);
    } public int getFaceValue()
    {
    return faceValue;
    } private int faceValue;
    }class DiceGame
    {
    public boolean play()
    {
    die1 = new Die();
    die2 = new Die();
    die1.roll();
    die2.roll();
    System.out.println("die1.getFaceValue()"+"+"+"die2.getFaceValue()"+"="+(int)(die1.getFaceValue() + die2.getFaceValue()));
    if (die1.getFaceValue() + die2.getFaceValue() == 7)
    {
    return true;
    }
    else
    {
    return false;
    }
    } private static Die die1;
    private static Die die2;
    };public class TestFace
    {
    public static void main (String [] args)
    {
    DiceGame dg = new DiceGame();
    if (dg.play())
    {
    System.out.println("您赢了!");

    else
    {
    System.out.println("您输了!");
    }
    }PS:终于可以睡觉了!
      

  4.   

    先帖我的代码吧:import java.util.Random;class Die {
    /**
     * 取得随机骰子值的方法
     * @return 骰子值-faceValue
     */
    public int getFaceValue() {
    faceValue = new Random().nextInt(6) + 1;
    System.out.println("骰子值= " + faceValue);
    return faceValue;
    }

    private int faceValue;
    }class DiceGame {
    /**
     * @param x
     * @param y
     * @return 如果2次骰子值之和=7就是true
     */
    public boolean play(Die x, Die y) {
    return (x.getFaceValue() + y.getFaceValue() == 7);
    }
    }public class TestFace {
    public static void main(String[] args) // throws Exception
    {
    DiceGame dg = new DiceGame();
    Die die1 = new Die();
    Die die2 = new Die();
    if (dg.play(die1, die2)) {
    System.out.println("您赢了,请客吧!");
    } else
    System.out.println("您输了,回家吧!");
    }
    }
    LZ可用慢慢玩骰子了。。
      

  5.   

    也不是不可以用静态,关键是用静态之后记得把变量保存下来die1.roll();
    int v1=die1.getFaceValue();
    die2.roll();
    int v2=die2.getFaceValue();if(v1+v2==7)
      //赢
    else
      //输
      

  6.   

    静态变量不是不可以用,而是你用的不正确package ch03;/*
    3、 用Java实现如下的骰子游戏:丢下两个骰子,若分值的总值为7点,则赢;否则输。提示:
    1)首先定义Die类。
    提示:Die类表示一个骰子有faceValue一个静态属性,有一个roll()方法getFaceValue()。
    roll()方法使faceValue为1~6中的一个随机值。getFaceValue()是取出faceValue值2)然后定义DiceGame类。
    提示:DiceGame类有die1、die2两个静态属性,有一个play()方法。play()方法返回一个布尔类型,true表示分值的总值为7点,否则为false。3)最后写了同个Test类,对上面定义的类进行测试。
    提示:写出主类,main()方法中产生DiceGame对象,执行play()方法后显示出输赢*/import java.util.Random;
    class Die
    {
        public void roll()
        {
            Random x = new Random();
            faceValue = (int)(x.nextDouble()*6+1);
            System.out.println(faceValue);
        }    public int getFaceValue()
        {
            return faceValue;
        }    private static int faceValue;
    }class DiceGame
    {
        public boolean play(Die x,Die y)
        { x.roll();
         die1=x.getFaceValue();
         y.roll();
         die2=x.getFaceValue();
         System.out.println("die1="+die1+" die2="+die2);
            if (die1 + die2 == 7)
            {
                return true;
                //System.out.println("您赢了!");
            }
            else
                //System.out.println("您输了!");
            return false;
        }    private static int die1;
        private static int die2;
    };public class TestFace
    {
        public static void main (String [] args) //throws Exception
        {
            //try{
            DiceGame dg = new DiceGame();
            Die die1 = new Die();
            Die die2 = new Die();
           // die1.roll();
           // die2.roll();
           if (dg.play(die1,die2))
            {
                System.out.println("123");
                System.out.println("您赢了");
            }
           else
            System.out.println("您输了");
            System.out.println(die1.getFaceValue() + die2.getFaceValue());
            /*}
            catch (NullPointerException e)
            {
                System.out.println(e.getMessage());
            }*/
        }
    };