2016-02-24 86 views
0

好吧,我正在製作一個簡單的基於文本的遊戲,我不確定爲什麼和無限循環正在創建。它並不是真正無限的,但我不確定爲什麼if語句不是在每個循環中進行評估。這是整個程序。我需要修改的if語句位於代碼末尾的roomEight方法中。爲什麼if語句沒有在每個循環中評估

//******************************** 
// A simple game that moves the 
// player though the map 
//******************************** 
import java.util.*; 
import java.text.*; 

public class mazegame 
{ 
    private static Scanner scan = new Scanner (System.in); // starts scanner for the program 
    public static Scanner scanS; 
    // ScanS is a scanner for strings. 
    // To call this variable type mazegame.scanP; 
     public static int lifeCount = 15; 
     public static int damage = 1; 
     // imp stats 
      public static int impAmount = 0; 
      public static int impDamage = 1; 
      public static int impLife = 1; 
       // Low level monster stats 
       // m followed by a number stands for monster then the level of monster 
       public static int m1health = 5; 
       public static int m1damage = 2; 
        // High level monster 
        public static int m2health = 10; 
        public static int m2damage = 2; 
         // Boss stats  
         public static int bosshealth = 30; 
         public static int bossdamage = 10; 
          // Placement of player 
          public static int placement = 3; 
          public static String movement; 
          public static int scanVal; // holder a scanner value generic. 
    public static void main(String[] args) 
    { 
     System.out.println("You wake up on a cold hard floor"); 
     time(); 
     System.out.println("you are unsure how you got there."); 
     time(); 
     System.out.println("There is an opening a head"); 
     time(); 
     System.out.println("you walk forward into the opening the ground begins to tremble"); 
     time(); 
     System.out.println("the wall behind you closes you are trapped."); 
     time(); 
     time(); 
     clear(); // clears screen for user. 
     roomThree(); 

    } 
    public static void timeHalfSec() 
    { 
     try 
      { 
       Thread.sleep(500);    //1000 milliseconds is one second. 
      }catch(InterruptedException ex) 
       { 
        Thread.currentThread().interrupt(); 
       } 
    } 
    public static void time() 
    { 
     try 
      { 
       Thread.sleep(1500);    //1000 milliseconds is one second. 
      }catch(InterruptedException ex) 
       { 
        Thread.currentThread().interrupt(); 
       } 
    } 
    public static void clear() 
    { 
     final String ANSI_CLS = "\u001b[2J"; 
     final String ANSI_HOME = "\u001b[H"; 
     System.out.print(ANSI_CLS + ANSI_HOME); 
     System.out.flush(); 
    } 
    public static void position(int placement) 
    { 
     switch(placement) 
     { 
      //******************************** 
      // For each room create a method and 
      // call it in this switch statement. 
      //******************************** 
      case 1: 
       break; 
      case 2: 
       break; 
      case 3: 
       break; 
      case 4: 
       break; 
      case 5: 
       break; 
      case 6: 
       break; 
      case 7: 
       break; 
      case 8: roomEight(); 
       break; 
      case 9: 
       break; 
      case 10: 
       break; 
      case 11: 
       break; 
      case 12: 
       break; 
      case 13: 
       break; 
      case 14: 
       break; 
      case 15: 
       break; 
      case 16: 
       break; 
      case 17: 
       break; 
      case 18: 
       break; 
      case 19: 
       break; 
      case 20: 
       break; 
      case 21: 
       break; 
      case 22: 
       break; 
      case 23: 
       break; 
      case 24: 
       break; 
      case 25: 
       break; 
     } 
    } 
    public static void askMove() 
    { 
     System.out.println("You can walk forward, left , or right. Beware the imps."); 
     System.out.println("Enter L for left, R for right, and F for forward."); 
     time(); 
     System.out.print("Move:"); 
     movement = scan.nextLine(); 
    } 
    public static void roomThree() 
    { 
     askMove(); 
     //-------------------------------- 
     // This switch stament is only for this room 
     //-------------------------------- 
     switch (movement) 
     { 
      case "l": 
      case "L": 
       placement = 2; 
       System.out.println("Changing rooms Please wait"); 
       time(); 
       clear(); 
       break; 
      case "r": 
      case "R": 
       placement = 4; 
       System.out.println("Changing rooms Please wait"); 
       time(); 
       clear(); 
       break; 
      case "f": 
      case "F": 
       placement = 8; 
       System.out.println("Changing rooms Please wait"); 
       time(); 
       clear(); 
       break; 
     } 
     // The switch statement changes position and position calls the next room method. 
     position(placement);  
    } 
    public static void roomEight() 
    { 
      System.out.print ("You have just entered a new room."); 
      System.out.print ("There is an imp ahead would you like to see its stats? 1 for yes and 0 "); 
      impAmount = 1; 
      scanVal = scan.nextInt(); 
      if(scanVal == 1) 
      { 
       impStats(); 
      } 
      System.out.println("Would you like to hit the imp? 1 for yes and 0 for no."); 
      scanVal = scan.nextInt(); 
      while (impAmount != 0) 
      { 
       if (scanVal == 1) 
       { 
        impAmount = 0; 
        damage++; 
        lifeCount = 15; 
        System.out.println("You killed an imp! You found brass knuckles your damage increased by one. Here are your stats"); 
        playerStats(); 

       }else{ 
         lifeCount--; 
         System.out.println("The imp hit you. You took one damage"); 
         playerStats(); 
         timeHalfSec(); 
         dead(); 
        } 
      } 

    } 
    public static void playerStats() 
    { 
     System.out.println("*----------------*"); 
     System.out.println("Your Hearts: " + lifeCount); 
     System.out.println("Your Damage: " + damage); 
     System.out.println("*----------------*"); 
    } 
    public static void impStats() 
    { 
     System.out.println ("*----------------*"); 
     System.out.println("Amount of Imps: " + impAmount); 
     System.out.println("Imp Health: 1"); 
     System.out.println("impDamage: 1"); 
     System.out.println("*----------------*"); 
    } 
    public static void dead() 
    { 
     if(lifeCount < 1) 
     { 
      System.exit(0); 
     } 
    } 



} 



//******************************************************************************************************************************** 
// Places to look for code and things to look up. 
// Lookup: .equalsIgnoreCase, global scanner. 
// Places to look for code: 
// http://stackoverflow.com/questions/23586732/how-to-make-a-one-static-scanner-global-variable-without-closing-scan-constantly 
// https://www.youtube.com/watch?v=zijvKOjnmwY 
// http://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 
// http://www.csci.csusb.edu/dick/samples/java.classes.html#System 
// http://stackoverflow.com/questions/22452930/terminating-a-java-program 
// 
// 
// 
//******************************************************************************************************************************** 

回答

0

你可能會希望下面兩行進入循環:

System.out.println("Would you like to hit the imp? 1 for yes and 0 for no."); 
scanVal = scan.nextInt(); 
+0

即使我把它放在循環中,它也不應該做任何事情。如果你可以編譯和運行代碼,你會看到什麼是錯的。如果您選擇其他選項來擊中imp,即開始重複直到玩家的生命值小於1。但由於某種原因,我不確定爲什麼if語句不在每個循環中進行評估。 –

+0

omg哇這是一個簡單的錯誤,我應該看到謝謝你的幫助 –

0

正如我所看到的,你掃描nextInt,用戶輸入,你做的東西與它,然後你重新掃描nextInt。問題在於當你使用掃描器並要求一個int時,掃描器中仍然有新行字符('\ n')。因此,當你再次詢問int時,這將返回新的行char。我的這一切認識不上點,但你必須做的就是這些解決方案之一:

  1. 使用nextLine代替nextInt和解析字符串值轉換成int類型。這將清除緩衝區,並且您將能夠驗證用戶是否輸入了有效的Int。後scan.nextInt()

希望這有助於

String scanVal = scan.nextLine(); 
//You can add some try catch in order to validate the int being parsed 
int choice = Integer.parseInt(scanVal); 
  • 或者,您可以清除緩衝區已掃描您的INT後,通過調用scan.nextLine():你會不喜歡這樣!

  • +0

    *「這個問題是當你使用掃描儀,並要求一個單一的int,仍然是在掃描儀中的新行字符'\ n')。因此,當你再次詢問int時,這將返回新的一行字符。「*不,這是不正確的。您可能想要閱讀以下內容:[在nextXXX之後使用nextLine時出現掃描程序問題](http://stackoverflow.com/q/7056749) – Tom

    +0

    與我所說的非常接近,我認爲nextInt正受到剩餘換行符char的影響,道歉.. – RaphBlanchet

    +0

    你仍然可以更新你的答案。 – Tom