Java 从另一个方法调用的布尔方法

Java 从另一个方法调用的布尔方法,java,methods,boolean,Java,Methods,Boolean,我想在java上完成一个小游戏。 这场游戏是基于多种方法的。 第一种方法是布尔方法,根据我在游戏中是否有足够的生命来判断是真是假 public boolean removive(){ 这个.wastedLives++; if(this.wastedLives==this.lives){ 剩余生命=虚假; System.out.println(“游戏结束”); } 如果(this.wastedLivesthis.randomN){ System.out.println(“你也找到了一个较低的数字”

我想在java上完成一个小游戏。 这场游戏是基于多种方法的。 第一种方法是布尔方法,根据我在游戏中是否有足够的生命来判断是真是假

public boolean removive(){
这个.wastedLives++;
if(this.wastedLives==this.lives){
剩余生命=虚假;
System.out.println(“游戏结束”);
}
如果(this.wastedLives
因此,如果我引用这个方法,它会做应该做的事情,因为在其他方法(calculateLives)中,我实现了生命的价值=惊人的浪费生命

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

public void Play(){
super.rebootGame();
布尔继续=真;
System.out.println(“选择一个介于1和100之间的数字:”);
this.myNumber=Integer.parseInt(sc.nextLine());
而(this.myNumber<1 | | this.myNumber>100){
System.out.println(“再次选择范围内的数字”);
this.myNumber=Integer.parseInt(sc.nextLine());
}
this.randomN=generateRandomNumber();//生成随机数(int)(Math.random()*100)+1的方法;
设定开始寿命(5);
while(continue==true&&removeLive()==true){
if(this.myNumber==this.randomN){
System.out.println(“U WIN!!”);
刷新记录();
continue=false;
}否则{
removeLive();
if(continue==true&&removeLive()==true){
如果(this.myNumber>this.randomN){
System.out.println(“你也找到了一个较低的数字”);
}否则{
System.out.println(“你也找到了一个更高的数字”);
}
this.myNumber=Integer.parseInt(sc.nextLine());
}                          
}   
}
}
问题是第一个方法,布尔方法,似乎没有被调用,我不知道为什么。因为我设定了开始的生命,每次我都要求重新开始,我猜不到随机数


提前感谢,并为糟糕的翻译道歉。

好的,我对CoVid19有点笨,所以太多的人在屏幕前帮助我保持清醒的头脑

首先,第一个如果刚好在第二个的下方,而需要另一个条件,因为没有它,它将处于无限循环中

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;
                    }**
我错过的第二件事。 我的boolean方法返回一个boolean,但我所做的每一个调用都会有一个live。所以我需要一个getter方法来获取布尔值

public boolean getRestingLives(){
        return restingLives;
    }
第三,同样重要的是,我打错了超级参数。 所以我加了一个合适的电话

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

很抱歉把思路弄乱了,但有时有必要提出问题以获得自己的答案

根据堆栈溢出的原理,它不仅是一个写正确答案的地方,也是一个教育新程序员的地方,我正在写一些笔记,应该可以帮助您解决问题。即使纠正了这些错误,你也需要进一步的帮助,请随意评论,我会尽力的

1。您不应使用Java关键字作为变量名,例如,将变量命名为
continue
是错误的。将其重命名为其他名称,例如
cont

2.您应该遵循例如方法名称,
Play
应该是
Play
。虽然这不会以任何方式影响你的课程,但你的老师/考官会因为没有遵守惯例而扣减一些分数

3.以下代码构造是执行您想要执行的操作的不正确方式:

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());
}

请注意,这只是一个建议,不会对您的计划产生任何影响

5。请注意,在下面给出的代码中,您调用了两次
removeLive()
,而您可能只打算调用它一次

} else{
    removeLive();
    if(continue==true&&removeLive()==true){
        //...
    }
    //...
}

“continue”是java保留关键字,不要将其用作变量。使用其他类似“continueGame”的方法为什么您认为该方法没有被调用?在游戏开始之前,你可能已经在循环中调用了这个方法。正如@AtulKumarVerma所说,
continue
是一个java保留关键字,我很惊讶您的类甚至编译了Yep。Atul是对的,但它是从西班牙语翻译过来的continue(在这个程序中,我只是称之为continua)。还有Marcos Barbero,你也是对的,我给这个方法打了好几次电话。我修正了两个错误。谢谢你们留下来帮忙。@rafanarvaiza-我希望这个解决方案对你们有用。不要忘记接受答案,这样将来的访问者也可以自信地使用解决方案。检查以了解如何做。如有任何疑问/问题,请随时发表评论。
while (cont == true && removeLive() == true)
while (cont && removeLive())
} else{
    removeLive();
    if(continue==true&&removeLive()==true){
        //...
    }
    //...
}