Java 我的游戏代码中不需要的无限循环。如何摆脱它?
到目前为止,大部分代码我都是自己编出来的。我想做一个基于文本的小游戏。我遇到的问题是,我似乎无法使随机数滚动正确循环。当它循环时,它只是循环最后一个滚动的东西。这是我的代码,全部内容Java 我的游戏代码中不需要的无限循环。如何摆脱它?,java,loops,Java,Loops,到目前为止,大部分代码我都是自己编出来的。我想做一个基于文本的小游戏。我遇到的问题是,我似乎无法使随机数滚动正确循环。当它循环时,它只是循环最后一个滚动的东西。这是我的代码,全部内容 import java.util.Scanner; import java.util.Random; //Setting the variables for monsters class monster { int health; String name; int damage; } /
import java.util.Scanner;
import java.util.Random;
//Setting the variables for monsters
class monster {
int health;
String name;
int damage;
}
//Setting the Variables that the player has
class Player {
String name;
int health;
int damage;
}
public class Main {
public static void main(String[] args) {
//Creating a scanner to receive user input
Scanner scanner1 = new Scanner(System.in);
//Creating Monsters
monster Goblin = new monster();
Goblin.name = "Goblin";
Goblin.health = 5;
Goblin.damage = 1;
monster Orc = new monster();
Orc.name = "Orc";
Orc.health = 8;
Orc.damage = 3;
//Creating player stats
Player Player = new Player();
Player.name = "";
Player.health = 15;
Player.damage = 3;
//////////////////////////////////////
//Beginning of the game
System.out.println("Hello adventurer. What is your name?");
//Receive user input for their name
String Username = scanner1.nextLine();
Player.name = Username;
//Message to restate player's name
System.out.println("Ah, so your name is " + Username + ". Pleasure to meet you.");
//Continuing description.
System.out.println("Sorry to say, but there are more pressing matters at hand.");
System.out.println("The nearby town of Notsosafe has been overrun by monsters!");
System.out.println("Please go and help clear out the monsters there.");
System.out.println("What is your weapon of choice? The Sword and shield? Greatsword? or Staff?");
//Check for user input
String weapon = scanner1.nextLine();
//Check for which stats to apply
while(true)
if(weapon.equals("Sword and shield")){
Player.damage = 3 + 2;
Player.health = 15 + 3;
break;
}
else if(weapon.equals("Greatsword")){
Player.damage = 3 + 5;
break;
}
else if(weapon.equals("Staff")){
Player.damage = 3 + 1;
Player.health = 15 + 5;
break;
}
else{
System.out.println("We don't seem to have that, Please pick what we have.");
weapon = scanner1.nextLine();
}
//Displays the Player's stats
System.out.println("Your stats are now:");
System.out.println("Attack: " + Player.damage);
System.out.println("Health: " + Player.health);
System.out.println(" ");
System.out.println("Now that you have selected your weapon, Go and save the town of Notsosafe!");
//Rolls random number to decide encounter
Random dice = new Random();
int randomNum = dice.nextInt((6-1) + 1) + 1;
System.out.println(randomNum);
int roll = 0;
//Actions corresponding to random roll and which actions to take
while(true)
if(roll < 10){
switch(randomNum){
case 1:
System.out.println("You've traveled some miles without any encounter.");
break;
case 2:
System.out.println("You've traveled some miles without any encounter.");
break;
case 3:
System.out.println("You've traveled some miles without any encounter.");
break;
case 4:
System.out.println("You've run into a goblin! Attack or Defend?");
String action = scanner1.nextLine();
while(Goblin.health > 0){
if(action.equals("Attack")){
Goblin.health = Goblin.health - Player.damage;
System.out.println("You've done " + Player.damage + " damage to Goblin. Goblin has "+ Goblin.health + " health remaining.");
while(true){
if(Goblin.health <= 0){
System.out.println("You have slain the Goblin! Continue to move forward.");
break;
}
else{
Player.health = Player.health - Goblin.damage;
System.out.println("Goblin attacked you for " + Goblin.damage + " damage.");
System.out.println("You have " + Player.health + " health remaining.");
System.out.println("Will you Attack or Defend?");
action = scanner1.nextLine();
if(action.equals("Attack")){
Goblin.health = Goblin.health - Player.damage;
System.out.println("You've done " + Player.damage + " damage to Goblin. Goblin has "+ Goblin.health + " health remaining.");
continue;
}
}
}
}
else if(action.equals("Defend")){
System.out.println("You defended against the attack.");
System.out.println("Goblin attacked you for " + Goblin.damage/2 + " damage.");
System.out.println("You have " + Player.health + " health remaining.");
Goblin.health = Goblin.health - Player.damage/2;
System.out.println("You counter-attack for " + Player.damage/2 + " damage. Goblin has " + Goblin.health + " health remaining");
Player.health = Player.health - Goblin.damage/2;
if(Goblin.health <= 0){
System.out.println("You have slain the Goblin! Continue to move forward.");
break;
}
else{
System.out.println("Will you Attack or Defend?");
action = scanner1.nextLine();
if(action.equals("Defend")){
continue;
}
}
}
}
break;
case 5:
System.out.println("You've run into a goblin! Attack or Defend?");
String action1 = scanner1.nextLine();
while(Goblin.health > 0){
if(action1.equals("Attack")){
Goblin.health = Goblin.health - Player.damage;
System.out.println("You've done " + Player.damage + " damage to Goblin. Goblin has "+ Goblin.health + " health remaining.");
while(true){
if(Goblin.health <= 0){
System.out.println("You have slain the Goblin! Continue to move forward.");
break;
}
else{
Player.health = Player.health - Goblin.damage;
System.out.println("Goblin attacked you for " + Goblin.damage + " damage.");
System.out.println("You have " + Player.health + " health remaining.");
System.out.println("Will you Attack or Defend?");
action1 = scanner1.nextLine();
if(action1.equals("Attack")){
Goblin.health = Goblin.health - Player.damage;
System.out.println("You've done " + Player.damage + " damage to Goblin. Goblin has "+ Goblin.health + " health remaining.");
continue;
}
}
}
}
else if(action1.equals("Defend")){
System.out.println("You defended against the attack.");
System.out.println("Goblin attacked you for " + Goblin.damage/2 + " damage.");
System.out.println("You have " + Player.health + " health remaining.");
Goblin.health = Goblin.health - Player.damage/2;
System.out.println("You counter-attack for " + Player.damage/2 + " damage. Goblin has " + Goblin.health + " health remaining");
Player.health = Player.health - Goblin.damage/2;
if(Goblin.health <= 0){
System.out.println("You have slain the Goblin! Continue to move forward.");
break;
}
else{
System.out.println("Will you Attack or Defend?");
action1 = scanner1.nextLine();
if(action1.equals("Defend")){
continue;
}
}
}
}
break;
case 6:
System.out.println("You've run into an Orc! Attack or Defend?");
String action2 = scanner1.nextLine();
while(Orc.health > 0){
if(action2.equals("Attack")){
Orc.health = Orc.health - Player.damage;
System.out.println("You've done " + Player.damage + " damage to Orc. Orc has "+ Orc.health + " health remaining.");
while(true){
if(Orc.health <= 0){
System.out.println("You have slain the Orc! Continue to move forward.");
break;
}
else{
Player.health = Player.health - Orc.damage;
System.out.println("Orc attacked you for " + Orc.damage + " damage.");
System.out.println("You have " + Player.health + " health remaining.");
System.out.println("Will you Attack or Defend?");
action2 = scanner1.nextLine();
if(action2.equals("Attack")){
Orc.health = Orc.health - Player.damage;
System.out.println("You've done " + Player.damage + " damage to Orc. Orc has "+ Orc.health + " health remaining.");
continue;
}
}
}
}
else if(action2.equals("Defend")){
System.out.println("You defended against the attack.");
System.out.println("Orc attacked you for " + Orc.damage/2 + " damage.");
System.out.println("You have " + Player.health + " health remaining.");
Orc.health = Orc.health - Player.damage/2;
System.out.println("You counter-attack for " + Player.damage/2 + " damage. Orc has " + Orc.health + " health remaining");
Player.health = Player.health - Orc.damage/2;
if(Orc.health <= 0){
System.out.println("You have slain the Goblin! Continue to move forward.");
break;
}
else{
System.out.println("Will you Attack or Defend?");
action2 = scanner1.nextLine();
if(action2.equals("Defend")){
continue;
}
}
}
}
roll = roll + 1;
}
}
else{
break;
}
}
}
在你杀死了地精之后,它只是循环问题,而不是实际返回并运行伤害代码等等。我做错了什么?这是因为你没有在你的地精死亡后重置它的生命。您的代码非常庞大,但以下是您的代码无法按预期工作的原因:
//您只能在范围中创建一个Goblin实例
怪物地精=新怪物();
Goblin.name=“Goblin”;
地精:健康=5;
地精伤害=1;
...
while(true)
如果(滚动<10){
开关(随机数){
...
案例4:
System.out.println(“你遇到了一个地精!攻击还是防御?”);
String action=scanner1.nextLine();
while(Goblin.health>0){
if(action.equals(“Attack”){
Goblin.health=Goblin.health-Player.damage;
System.out.println(“你对地精造成了“+玩家伤害+”伤害。地精拥有“+地精健康+”剩余健康。”);
while(true){
如果(Goblin.health是正确的,则不会重置生命值,因此它会卡在循环中
while(Goblin.health > 0)
我想补充更多的答案。避免总是使用下面的
while(true)
如果你没有正确地中断,它会导致一个无限循环
boolean something = true
while(something)
{
//do something
}
因此,您可以在需要时轻松地将标志设置为false
而且,当你进入循环时,你得到了
while(true)
if(roll < 10){
}
尝试将所有事件分解为方法,以便调试和发现问题,我希望您使用的是调试器(调试时很容易发现)变量roll
不是递增的,因此您将无休止地评估开关(randomNum)
语句,其值为不变的randomNum
。这就是为什么即使在敌人被杀后,它仍会重复相同的情况。我的循环有什么错?[…]这是我的代码,全部内容。你有3个循环。你说的是哪一个循环?你真的应该把你的代码分解成至少更多的函数…对不起,我忘了提到哪个。我相信我遇到问题的是包含开关的那一个。请原谅我发布了全部内容。我只是不确定我是否这里还有其他错误。对不起,我只是在用普通的。我对编码还是很陌生,所以我对这一切都不熟悉。谢谢你提供的信息。我会试一试的。
while(true)
if(roll < 10){
}
while (roll != 10)
{
}