java吧 关注:1,296,109贴子:12,830,003
  • 7回复贴,共1

This method must return a result of type boolean求大神

只看楼主收藏回复

public class Game {
public static void main(String[] args) {
int i,j,k=0,l;
for(l=0;l<10000;l++) {
i=random();
j=random();
if(play(i,j))
k++;
}
System.out.println("你共赢了"+k+"局");
}
public static boolean play(int a,int b) {
int c;
c=a+b;
if(a+b==2||a+b==3||a+b==12)
return false;
else if(a+b==7||a+b==11)
return true;
else {
if(playagain(a,b,c)==1)
return true;
else if(playagain(a,b,c)==0)
return false;
}
}
public static int random() {
int c;
c=(int)(1+Math.random()*6);
return c;
}
public static int playagain(int c,int d,int e) {
c=random();
d=random();
if(c+d==e)return 1;
else if(c+d==7)return 0;
else {e=c+d;playagain(c,d,e);}
}
}
第二个函数和第四个函数提示没有返回值。


IP属地:泰国1楼2013-03-24 20:59回复
    这个方法必须返回一个布尔型的结果。


    3楼2013-03-24 21:02
    收起回复
      2026-01-28 17:03:15
      广告
      不感兴趣
      开通SVIP免广告
      如果用else if是不能直接return的。
      可以定义一个boolean值
      boolean sth = true;
      if(.....){
      sth = true;
      }else if(.....){
      sth = false;
      }
      return sth;
      这样写应该可以。两个都是一样的问题,试一试。


      IP属地:北京4楼2013-03-25 09:57
      回复


        IP属地:江苏5楼2013-08-29 14:48
        回复
          其实我想问你的playagain直接把数字掩盖了还要它们做啥


          来自Android客户端6楼2014-03-20 13:25
          回复