A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王勇文 中级黑马   /  2013-1-21 16:38  /  1863 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张向辉 于 2013-1-22 12:49 编辑

.      用面向对象的思想实现如下的骰子游戏:
      丢下两个骰子,若分值的总值为
7点,则赢;否则输。

       对 面向对象思想的理解不透彻,多多谅解。 希望有答案。

面向对象思想.jpg (9.47 KB, 下载次数: 34)

面向对象思想.jpg

评分

参与人数 1黑马币 +9 收起 理由
Rancho_Gump + 9 赞一个!

查看全部评分

5 个回复

倒序浏览
http://zhidao.baidu.com/question/161443213.html
回复 使用道具 举报
package cn.itzero;

public class Play {

        /**
         * @param args
         */
        public static void main(String[] args) {

                Deic a=new Deic();//定义a.b色子
                Deic b=new Deic();
                if(a.getNum()+b.getNum()==7){
                        System.out.println("win");
                }else{
                        System.out.println("lose");
                }
        }
}
class Deic{
        private int num;
        public Deic(){
                num=(int) (Math.random()*6+1);//获取随机数
        }
        public int getNum() {
                return num;
        }
       
}
你看看,像这样就行了

评分

参与人数 1黑马币 +9 收起 理由
Rancho_Gump + 9 赞一个!

查看全部评分

回复 使用道具 举报
  1. class Test1{

  2.         public static void main(String[] args){
  3.                
  4.                  Die die1 = new Die();
  5.                  Die die2 = new Die();
  6.                  
  7.                  int sum = play(die1,die2);
  8.                  
  9.                  if(sum == 7)
  10.                          System.out.println("win");
  11.                  else
  12.                          System.out.println("lost");
  13.                  
  14.         }

  15.         public static int play(Die die1,Die die2) {
  16.                 int sum = die1.getFaceValue() + die2.getFaceValue();
  17.                 return sum;
  18.         }
  19. }

  20. class Die {
  21.        
  22.         private int faceValue;
  23.        
  24.         public int getFaceValue() {
  25.                 int number = this.roll();
  26.                 return number;
  27.         }
  28.        
  29.         private int roll() {
  30.                 int a = 0;
  31.                 while(true)        {
  32.                         a = (int) (Math.random()*10);
  33.                         if(a>0 && a<7) {            //只有在1-6点间才能返回值
  34.                                 break;
  35.                         }
  36.                 }
  37.                 return a;
  38.         }
  39. }
复制代码

评分

参与人数 1黑马币 +9 收起 理由
Rancho_Gump + 9 赞一个!

查看全部评分

回复 使用道具 举报
写的不好,见谅啊
  1. import java.util.Random;


  2. public class ThrowDice {
  3.         public static void main(String[] args) {
  4.                 Dice dice1 = new Dice(1, 2, 3, 4, 5, 6);
  5.                 Dice dice2 = new Dice(1, 2, 3, 4, 5, 6);
  6.                 while(true) {
  7.                         if(getSum(dice1, dice2) == 7) {
  8.                                 System.out.println("你赢了");
  9.                                 break;
  10.                         } else {
  11.                                 System.out.println("你输了");
  12.                         }
  13.                 }
  14.         }
  15.         public static int getSum(Dice dice1, Dice dice2){
  16.                 int i = dice1.throwDice();
  17.                 int j = dice1.throwDice();
  18.                 return i + j;
  19.         }
  20.        
  21. }


  22. class Dice {                //定义一个类,代表骰子
  23.         public static final Random r = new Random();                //定义一个随机数产生器,将其声明为常量
  24.         private int suface1;                //定义6个成员变量,代表骰子上的每一个面的点数
  25.         private int suface2;
  26.         private int suface3;
  27.         private int suface4;
  28.         private int suface5;
  29.         private int suface6;
  30.         public Dice(int suface1, int suface2, int suface3, int suface4,
  31.                         int suface5, int suface6) {                //这个构造方法代表你要创造的骰子每个面对应的点数,貌似有点多余
  32.                 super();
  33.                 this.suface1 = suface1;
  34.                 this.suface2 = suface2;
  35.                 this.suface3 = suface3;
  36.                 this.suface4 = suface4;
  37.                 this.suface5 = suface5;
  38.                 this.suface6 = suface6;
  39.         }
  40.         public int throwDice() {                //定义一个方法,扔骰子
  41.                 return r.nextInt(6) + 1;
  42.         }
  43. }
复制代码

评分

参与人数 1黑马币 +12 收起 理由
Rancho_Gump + 12 赞一个!

查看全部评分

回复 使用道具 举报
游戏类:该类用于根据玩家的点数来输出结果
玩家类:有一个掷骰子的方法,该方法返回一个随机数。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马