| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 753 人关注过本帖
标题:求助,为什么每次运行结果都一样
只看楼主 加入收藏
lv104788313
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-4-16
结帖率:100%
收藏
 问题点数:0 回复次数:5 
求助,为什么每次运行结果都一样
public class GuessGame {
    Player p1;
    Player p2;
    Player p3;
    public void startgame(){
        p1=new Player();
        p2=new Player();
        p3=new Player();
        
        int guessp1=0;
        int guessp2=0;
        int guessp3=0;
        
        boolean p1isright=false;
        boolean p2isright=false;
        boolean p3isright=false;
        
        int targetnumber=(int) (Math.random()*10);
        System.out.println("I'm thinking of a number between 0-9...");
        
        while(true){
            System.out.println("Number to guess is"+targetnumber);
        
        
        p1.guess();
        p2.guess();
        p3.guess();
        
        guessp1=p1.number;
        System.out.println("Player one guessed"+guessp1);
        
        guessp2=p2.number;
        System.out.println("Player one guessed"+guessp2);
        
        guessp3=p3.number;
        System.out.println("Player one guessed"+guessp3);
        
        if(targetnumber==guessp1){
            p1isright=true;
        }
        if(targetnumber==guessp2){
            p2isright=true;
        }
        if(targetnumber==guessp3){
            p3isright=true;
        }
        if(p1isright||p2isright|| p3isright){
            System.out.println("We have a winner");
            System.out.println("Player one got it right?"+p1isright);
            System.out.println("Player two got it right?"+p2isright);
            System.out.println("Player three got it right?"+p3isright);
            System.out.println("Game is over!");
        }
        else{
            System.out.println("Players will have to try again!");
        }
        
        
        
        
        }
    }

}


public class GameLauncher {
    public static void main(String[] args) {
        GuessGame game=new GuessGame();
        game.startgame();

    }

}


public class Player {
    int number=0;
   
    public void guess(){
        number=(int)(Math.random()*10);
        System.out.println("I'm guessing"+number);
    }

}
搜索更多相关主题的帖子: false 
2011-04-19 20:56
lv104788313
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-4-16
收藏
得分:0 
谁来帮我解决一下,万分感谢
2011-04-20 10:56
machine_of_a
Rank: 2
等 级:论坛游民
帖 子:8
专家分:36
注 册:2011-4-19
收藏
得分:0 
每次循环应该将
      // 应该加上
      p1isright=false;
      p2isright=false;
      p3isright=false;
重新置false,否则每次运行结果都一样
程序代码:
class GuessGame {
  Player p1;
  Player p2;
  Player p3;
  public void startgame(){
    p1=new Player();
    p2=new Player();
    p3=new Player();
    int guessp1=0;
    int guessp2=0;
    int guessp3=0;
    boolean p1isright=false;
    boolean p2isright=false;
    boolean p3isright=false;
    int targetnumber=(int) (Math.random()*10);
    System.out.println("I'm thinking of a number between 0-9...");
       

    while(true){
      System.out.println("Number to guess is"+targetnumber);
      p1.guess();
      p2.guess();
      p3.guess();
      guessp1=p1.number;
     

      System.out.println("Player one guessed"+guessp1);       

      guessp2=p2.number;
      System.out.println("Player one guessed"+guessp2);       

      guessp3=p3.number;
      System.out.println("Player one guessed"+guessp3);
       

      // 应该加上
      p1isright=false;
      p2isright=false;
      p3isright=false;
     

      if(targetnumber==guessp1){
        p1isright=true;
      }
      if(targetnumber==guessp2){
        p2isright=true;
      }
      if(targetnumber==guessp3){
        p3isright=true;
      }
      if(p1isright||p2isright|| p3isright){
        System.out.println("We have a winner");
        System.out.println("Player one got it right?"+p1isright);
        System.out.println("Player two got it right?"+p2isright);
        System.out.println("Player three got it right?"+p3isright);
        System.out.println("Game is over!");
      }
      else{
        System.out.println("Players will have to try again!");
      }
    }
  }
}


public class test {

 public static void main(String[] args) {
    GuessGame game=new GuessGame();
    game.startgame();
  }
}

class Player {
  int number=0;
  

  public void guess(){
    number=(int)(Math.random()*10);
    System.out.println("I'm guessing"+number);
  }
}
2011-04-20 11:52
lv104788313
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-4-16
收藏
得分:0 
多谢了,解决了~!
2011-04-20 13:22
电脑小童
Rank: 2
等 级:论坛游民
帖 子:77
专家分:52
注 册:2010-10-16
收藏
得分:0 
请问楼上的取得什么名字啊 我怎么运行不了啊
2011-05-01 13:02
lv104788313
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-4-16
收藏
得分:0 
回复 5楼 电脑小童
一共三个类
GameLauncher
GuessGame
Player
2011-05-02 02:44
快速回复:求助,为什么每次运行结果都一样
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026792 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved