虽然我的布尔方法正确返回true或false,但我不能在main中使用return

时间:2013-02-28 06:17:16

标签: java methods boolean call

所以我的Boolean方法运行并返回正确的true或false ....但我需要某种扫描程序函数来获取返回if语句后面的内容。这是我的主要代码。任何帮助表示赞赏!此外,我的柜台不起作用:(我是新人。

import java.util.Random;
import java.util.Scanner;

public class CrapsP5 {
public static void main(String[] args) {

    int number = 0, total, counter;

    total = counter = 0;
    String response;
    Scanner console = new Scanner(System. in );
    //1
    System.out.println("Would you like to play craps? (Y/N)");
    response = console.next();
    if (response.equalsIgnoreCase("N")) {
        System.exit(0);
    }
    //2       
    System.out.println("Welcome to craps. Would you like to see the rules? (Y/N)");
    response = console.next();
    if (response.equalsIgnoreCase("Y")) {
        rules();
    }
    //3 call play method

    play();
    //I need something here to act like the scanner console does?? yes?? but what?
    if (true) {

        System.out.println("Congrats! You've won!");
        counter++; //add 1 to win count (w) 
        total = counter;
        play();

    }
    if (false) {
        System.out.println("I'm sorry, you've lost.");
        System.exit(0);
    }
    //4
    System.out.println("Thanks for playing! You won " + total + " number of times before     losing.");

3 个答案:

答案 0 :(得分:0)

这是你的布尔方法?玩()? 如果是的话

boolean result = play();
if(result){
...
}
else{
...
}

if(play()){
    ...
} else {
    ...
}

答案 1 :(得分:0)

尝试:

if(play())
{
  ....
}
else
{

}

如果你的play()返回布尔值。

答案 2 :(得分:0)

我想指出这一点:

play();
if (true) {

    System.out.println("Congrats! You've won!");
    counter++; //add 1 to win count (w) 
    total = counter;
    play();

}

说,用户第一次玩,如果他赢了,流进入条件区,并且进行另一次游戏调用。无论用户第二次赢或输第二次,计数器都会不要改变,游戏结束,执行:

System.out.println("Thanks for playing! You won " + total + " number of times before losing.");

我建议使用while循环继续播放,直到用户输掉。如:

while(play()){
    System.out.println("Congrats! You've won!");
    counter++; 
    total = counter;        
}
//It is assumed that the counter and eventually the game should restart on losing
System.out.println("I'm sorry, you've lost.");
System.out.println("Thanks for playing! You won " + total + " number of times before losing.");