2015-06-20 120 views
0

我正在尝试做一个非常简单的游戏(即使没有接口)。你应该输入你想要做什么动作,重,轻或阻止)。但“敌人”只对“轻微”行动作出反应。问题是什么?如何根据用户输入发送不同的动作

import java.util.Scanner; 
public class Game { 
    public static void main(String[] args) { 
     int hp = 10, enemy_hp = 10; 
     String attack = "attack"; 
     String block = "block"; 
     String heavy = "heavy"; 
     String light = "light"; 
     Scanner userInput = new Scanner(System.in); 
     while (enemy_hp > 0 && hp > 0) { 
      System.out.println("It is your turn, attack (heavy or light) or try to block"); 
      int your_block_chance1 = (int)(Math.random() * 4); //Chance to block an attack 
      int enemy_block_chance1 = (int)(Math.random() * 4); 
      String action = userInput.next(); 
      if (action.equals(attack)) { //attack 
       System.out.print("You attacked your enemy and "); 
       if (enemy_block_chance1 == 0) { 
        System.out.println("he blocked it"); 
       } else if (enemy_block_chance1 != 0) { 
        enemy_hp = enemy_hp - 3; 
        System.out.println("managed to hit, now his hp is " + enemy_hp); 
       } 
      } else if (action.equals(light)) { //light attack 
       System.out.print("You want to do a light attack"); 
       if (enemy_block_chance1 == 0) { 
        System.out.println(" but he blocked it"); 
       } else if (enemy_block_chance1 != 0) { 
        enemy_hp = enemy_hp - 1; 
        System.out.println(" and you managed to hit him, now his hp is " + enemy_hp); 
       } else if (action.equals(block)) { //block 
        System.out.println("You dicided to block and rest"); 
        your_block_chance1 = (int)(Math.random() * 1); 
        hp++; 
       } else if (action.equals(heavy)) { //heavy attack 
        System.out.print("You went for a heavy attack"); 
        int heavy_attack_chance = (int)(Math.random() * 2); 
        if (heavy_attack_chance == 1) { 
         System.out.println(" but failed"); 
        } else if (heavy_attack_chance != 1) { 
         if (enemy_block_chance1 == 0) { 
          System.out.println(" but he blocked it"); 
         } else if (enemy_block_chance1 != 0) { 
          enemy_hp = enemy_hp - 6; 
          System.out.println(" and you managed to hit you really hard, now his hp is " + enemy_hp); 
         } 
        } 
       } 
       System.out.print("It is your enemy turn, he decided to "); //enemy turn 
       int enemy_action2 = (int)(Math.random() * 4); 
       if (enemy_action2 == 1) { 
        System.out.print("attack you,"); //attack 
        if (your_block_chance1 == 0) { 
         System.out.println(" but you blocked it"); 
        } else if (your_block_chance1 != 0) { 
         hp = hp - 3; 
         System.out.println(" and you didn't block it, now your hp is " + hp); 

        } 
       } else if (enemy_action2 == 0) { //heavy attack 
        System.out.print("do a heavy attack"); 
        int heavy_attack_chance = (int)(Math.random() * 2); 
        if (heavy_attack_chance == 1) { 
         System.out.println(" but failed"); 
        } else if (heavy_attack_chance != 1) { 
         if (your_block_chance1 == 0) { 
          System.out.println(" but you blocked it"); 
         } else if (your_block_chance1 != 0) { 
          hp = hp - 6; 
          System.out.println(" and he managed to hit you really hard, now your hp is " + hp); 
         } 
        } 
       } else if (enemy_action2 == 3) { //light attack 
        System.out.print("do a light attack"); 
        if (your_block_chance1 == 0) { 
         System.out.println(" but you blocked it"); 
        } else if (your_block_chance1 != 0) { 
         hp = hp - 1; 
         System.out.println(" and he managed to hit you, now your hp is " + hp); 
        } 
       } else if (enemy_action2 == 2) { //block 
        System.out.println("block and rest"); 
        enemy_hp++; 
       } 
      } 
      if (hp <= 0) { 
       System.out.println("You failed"); 
      } else if (enemy_hp <= 0) { 
       System.out.println("You won!"); 
      } 
     } 
    } 
} 
+1

开始通过适当缩进你的代码;任何IDE和大多数文本编辑器都会为您做到这一点。跟随你的阻挡是非常困难的。 – chrylis

+0

请正确缩进您的代码。以当前形式阅读它非常困难 –

+0

对不起。现在好点了吗? –

回答

1

错放支架: 我从后面删除尾架},只是} else if (action.equals(heavy)) {

入住此之前添加一个:

import java.util.Scanner; 
public class Game1 { 
public static void main(String[] args) { 
    int hp = 10, enemy_hp = 10; 
    String attack = "attack"; 
    String block = "block"; 
    String heavy = "heavy"; 
    String light = "light"; 
    Scanner userInput = new Scanner(System.in); 
    while (enemy_hp > 0 && hp > 0) { 
     System.out.println("It is your turn, attack (heavy or light) or try to block"); 
     int your_block_chance1 = (int)(Math.random() * 4); //Chance to block an attack 
     int enemy_block_chance1 = (int)(Math.random() * 4); 
     String action = userInput.next(); 
     if (action.equals(attack)) { //attack 
      System.out.print("You attacked your enemy and "); 
      if (enemy_block_chance1 == 0) { 
       System.out.println("he blocked it"); 
      } else if (enemy_block_chance1 != 0) { 
       enemy_hp = enemy_hp - 3; 
       System.out.println("managed to hit, now his hp is " + enemy_hp); 
      } 
     } else if (action.equals(light)) { //light attack 
      System.out.print("You want to do a light attack"); 
      if (enemy_block_chance1 == 0) { 
       System.out.println(" but he blocked it"); 
      } else if (enemy_block_chance1 != 0) { 
       enemy_hp = enemy_hp - 1; 
       System.out.println(" and you managed to hit him, now his hp is " + enemy_hp); 
      } 
     } else if (action.equals(block)) { //block 
       System.out.println("You dicided to block and rest"); 
       your_block_chance1 = (int)(Math.random() * 1); 
       hp++; 
     } else if (action.equals(heavy)) { //heavy attack 
       System.out.print("You went for a heavy attack"); 
       int heavy_attack_chance = (int)(Math.random() * 2); 
       if (heavy_attack_chance == 1) { 
        System.out.println(" but failed"); 
       } else if (heavy_attack_chance != 1) { 
        if (enemy_block_chance1 == 0) { 
         System.out.println(" but he blocked it"); 
        } else if (enemy_block_chance1 != 0) { 
         enemy_hp = enemy_hp - 6; 
         System.out.println(" and you managed to hit you really hard, now his hp is " + enemy_hp); 
        } 
       } 
     } 
      System.out.print("It is your enemy turn, he decided to "); //enemy turn 
      int enemy_action2 = (int)(Math.random() * 4); 
      if (enemy_action2 == 1) { 
       System.out.print("attack you,"); //attack 
       if (your_block_chance1 == 0) { 
        System.out.println(" but you blocked it"); 
       } else if (your_block_chance1 != 0) { 
        hp = hp - 3; 
        System.out.println(" and you didn't block it, now your hp is " + hp); 

       } 
      } else if (enemy_action2 == 0) { //heavy attack 
       System.out.print("do a heavy attack"); 
       int heavy_attack_chance = (int)(Math.random() * 2); 
       if (heavy_attack_chance == 1) { 
        System.out.println(" but failed"); 
       } else if (heavy_attack_chance != 1) { 
        if (your_block_chance1 == 0) { 
         System.out.println(" but you blocked it"); 
        } else if (your_block_chance1 != 0) { 
         hp = hp - 6; 
         System.out.println(" and he managed to hit you really hard, now your hp is " + hp); 
        } 
       } 
      } else if (enemy_action2 == 3) { //light attack 
       System.out.print("do a light attack"); 
       if (your_block_chance1 == 0) { 
        System.out.println(" but you blocked it"); 
       } else if (your_block_chance1 != 0) { 
        hp = hp - 1; 
        System.out.println(" and he managed to hit you, now your hp is " + hp); 
       } 
      } else if (enemy_action2 == 2) { //block 
       System.out.println("block and rest"); 
       enemy_hp++; 
      } 
     } 
     if (hp <= 0) { 
      System.out.println("You failed"); 
     } else if (enemy_hp <= 0) { 
      System.out.println("You won!"); 
     } 
    } 

}

+0

@Napoleon Sraf:见编辑。输入“块”也被照顾。 –

1

这里当您在控制台输入 “重”

if (action.equals(attack)) { 

“重” 你的用户

while (enemy_hp > 0 && hp > 0) { 
     System.out.println("It is your turn, attack (heavy or light) or try to block"); 
     int your_block_chance1 = (int) (Math.random() * 4); //Chance to block an attack 
     int enemy_block_chance1 = (int) (Math.random() * 4); 
     String action = userInput.next(); 

输入不等于 “攻击”,所以它不用..

else if (action.equals(light)) { 

“重”不等于“光”s Ø再次

你会一路下滑到条件不成立:

 if (hp <= 0) { 
      System.out.println("You failed"); 
     } else if (enemy_hp <= 0) { 
      System.out.println("You won!"); 
     } 

所以它返回到while循环,你可以尝试再次键入。

您的“沉重”的条件是“轻”的条件内:

else if (action.equals(light)) { 
... 
... 
    } else if (action.equals(heavy)) { 

我觉得你的问题是,代码是没有可读性。尝试使用一些像intelij,eclipse这样的IDE来正确格式化你的代码。 我不知道你是否熟悉面向对象编程,但你应该使用更多的对象,可能就像玩家,敌人。您还可以添加特定的攻击类型或块类型的一些对象,有很多选择:)

相关问题