簡體   English   中英

盡管我的布爾方法正確返回true或false,但是我不能在main中使用return

[英]Though my boolean method returns true or false correctly, I can't use the return in main

因此我的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.");

您的布爾方法是哪一種? 玩()? 如果是這樣的話

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

要么

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

嘗試:

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

}

如果您的play()返回布爾值。

我想指出一點:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM