java - 如果在 Java 中不满足“X”值,则在循环中重新打印行
问题描述
如果不满足值,我正在尝试让我的代码重新打印一行。
我试过使用while
,但如果'X'不大于或等于1,它不会回到问题。
我目前正在尝试:
import java.util.Scanner;
public class rpg {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double hp = 10;
System.out.println("how much damage do you wish to do?");
double attack = input.nextDouble();
double damage = hp - attack;
System.out.println(damage);
System.out.println("health = " + Math.round(hp));
while (hp <= 1) {
System.out.println("Alive");
break;
}
}
}
但是当 hp 仍然大于 1 时,我无法重新说明问题。
解决方案
这应该可以阅读评论
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double hp = 10;
while(hp > 1) { //Moved The loop
System.out.println("how much damage do you wish to do?");
double attack = input.nextDouble();
//double damage = hp - attack;//not needed
hp = hp - attack;//Added this line
//System.out.println(damage);//not needed
System.out.println("health = " + Math.round(hp));
if(hp == 0)
System.out.println("Dead");
else
System.out.println("Alive");
}
}
推荐阅读
- arrays - 考虑到时间复杂度,如何将两个未排序的数组合并到另一个数组中
- python - 使用 python 抓取动态 javascript 内容网页
- python - 实时 Firebase 查询
- javascript - 正则表达式匹配各种时间实例
- rust - 如何同时运行包含借用 TcpStream 的期货?
- python - 如何使用子文件夹模板在 cookiecutter 模板中创建多个子文件夹
- python - comtypes ndarray 与 ASCOM safearray 问题
- architecture - Oracle ADF 是一条产品线吗?是什么让它成为一条产品线?
- c - 如何从用户输入中提取偶数,并将它们组合为C程序中的新数字
- java - Swing 原生外观与 AWT 风格