【问题标题】:Boolean method called from another method从另一个方法调用的布尔方法
【发布时间】:2020-07-02 07:54:56
【问题描述】:

我正在尝试在 java 上完成一个小游戏。 该游戏基于多种方法。 第一种方法,一个布尔值,根据我的游戏是否有足够的生命来判断我是真还是假。

public boolean removeLive(){
    this.wastedLives++;
    if(this.wastedLives == this.lives) {
        restingLives=false;
        System.out.println("GAME OVER");
    }
    if(this.wastedLives < this.lives) {
        restingLives=true;
    }
    return restingLives;
}

因此,如果我调用此方法,它会执行应做的操作,因为在其他方法(calculateLives)中,我实现了 lives=startLives-wastedLives 的值;

所以现在,我需要设计另一种生成随机数的方法,我必须猜测。

public void Play() {
    super.rebootGame(); 
    boolean continue=true;
    System.out.println("Choose a number between 1 &100: ");
    this.myNumber=Integer.parseInt(sc.nextLine()); 
    while(this.myNumber < 1 || this.myNumber > 100) {
        System.out.println("Choose again a number between the range");
        this.myNumber=Integer.parseInt(sc.nextLine());
    }
    this.randomN = generateRandomNumber();//Method that generate the random number (int)(Math.random()*100)+1;
    setStartingLives(5);
    while (continue == true && removeLive() == true) {
        if (this.myNumber == this.randomN) {
            System.out.println("U WIN!!");
            refreshRecord();
            continue = false;
        } else {
            removeLive();
            if(continue == true && removeLive() == true) {
                if(this.myNumber > this.randomN) {
                    System.out.println("U have too find a lower number");
                } else {
                    System.out.println("U have too find a higher number");
                }
                this.myNumber = Integer.parseInt(sc.nextLine());
            }                          
        }   
    }
}

问题是第一个方法,布尔方法,似乎没有被调用,我不知道为什么。因为我设置了起始生命,并且每次我都没有猜到随机数时,我都请求 removeLive。

在此先感谢,对翻译不佳表示歉意。

【问题讨论】:

  • "continue" 是 java 保留关键字,请勿将其用作变量。使用“continueGame”之类的其他内容
  • 你认为该方法为什么没有被调用?在游戏开始之前,您可能已经比您应该移除的生命更多,您已经在循环中调用了此方法。正如@AtulKumarVerma 所说,continue 是一个 java 保留关键字,我很惊讶你的类甚至可以编译
  • 是的。 Atul 是对的,但它是西班牙语 continue 的翻译(在程序中我只是调用 continua)。 Marcos Barbero,你也是对的,我多次调用该方法。我修复了这两个错误。感谢你们留在那里提供帮助。

标签: java methods boolean


【解决方案1】:

好吧,我对 CoVid19 有点笨,屏幕前我们的太多无助于头脑清醒。

首先,第二个 while 后面的第一个 IF 需要另一个条件,因为没有它,它将处于无限循环中。

while (continue==true&&removeLive()==true){
            if (this.myNumber==this.randomN){
                System.out.println("U WIN!!");
                refreshRecord();
                continue=false;
            } else{
                removeLive();
                if(continue==true&&removeLive()==true){
                    if(this.myNumber>this.randomN){
                    System.out.println("U have too find a lower number");
                    }
                    else{
                    System.out.println("U have too find a higher number");
                    }
                    this.myNumber=Integer.parseInt(sc.nextLine());
                }                          
            }   

并且它必须有另一个 if 或 else 来完成循环。

while (continue==true&&removeLive()==true){
            if (this.myNumber==this.randomN){
                System.out.println("U WIN!!");
                refreshRecord();
                continue=false;
            } else{
                removeLive();
                if(continue==true&&removeLive()==true){
                    if(this.myNumber>this.randomN){
                    System.out.println("U have too find a lower number");
                    }
                    else{
                    System.out.println("U have too find a higher number");
                    }
                    this.myNumber=Integer.parseInt(sc.nextLine());
                }                          
            }   **if(super.getRestingLives()==false){
                      continue==false;
                    }**

我错过的第二件事。 我的布尔方法返回给我一个布尔值,但我所做的每个调用,也都是休息。所以我需要一个getter方法来获取布尔值

public boolean getRestingLives(){
        return restingLives;
    }

第三个同样重要的是,我对 super.parameters 的调用是错误的。 所以我添加了正确的调用

        super.setStartingLives(this.startLives);
    super.calculateRestingLives(super.getStartingLives());

很抱歉弄乱了线程,但有时需要提出问题以获得自己的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多