【发布时间】: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,你也是对的,我多次调用该方法。我修复了这两个错误。感谢你们留在那里提供帮助。