问题描述:

so my Boolean method runs and returns the correct true or false....but I need some sort of scanner function to get what was returned to follow into the if statements. here is my main code. any help is appreciated! also, my counter doesn't work :( I'm new.

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.");

网友答案:

which is your boolean method? play()? If so

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

or

if(play()){
    ...
} else {
    ...
}
网友答案:

Try:

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

}

If your play() returns boolean.

网友答案:

I would like to point out this:

play();
if (true) {

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

}

says, the user plays for the first time, and if he wins, the flow gets into the conditional block, and another call to play takes place.No matter the user wins or loses the second time he plays, the counter does not change and the game ends, by executing:

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

I would suggest the use while loop to continue playing until the user loses. As in:

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.");
相关阅读:
Top