0

所以我的布尔方法运行并返回正确的真或假......但我需要某种扫描仪函数来获取返回的内容以遵循 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.");
4

3 回答 3

0

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

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

或者

if(play()){
    ...
} else {
    ...
}
于 2013-02-28T06:22:47.957 回答
0

尝试:

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

}

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

于 2013-02-28T06:29:19.313 回答
0

我想指出这一点:

play();
if (true) {

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

}

说,用户第一次玩,如果他赢了,则流程进入条件块,再次调用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.");
于 2016-11-02T15:41:37.237 回答