2017-07-21 32 views
0

所以首先只是一些文字,顯示我的代碼應該工作的思維過程。我在編程方面一般都很新,而且剛剛幾天前纔開始學習java。我想我對oop的工作原理有一個基本的瞭解,但是我無法將它很好地應用到我的代碼中。使用while循環使得一點意義,我也這樣想,我剛剛開始接觸那些; d如何在基於文本的遊戲Java中使用while循環?

事情我想:

帶有3個選項

菜單主菜單(營)

  1. 任務(激活任務環)

  2. 去鎮(激活鎮環)

  3. 退出遊戲(退出程序)

任務 戰鬥隨機怪物,並獲得金牌的遊戲循環/得分 「怪物出現」

  1. 打怪物 造成傷害怪物 怪物確實損壞回 回主迴路

  2. 使用項目 選擇項目 使用項目 項目效果應用於 回到主循環

  3. 運行 返回到主菜單(激活主菜單)

鎮 讓你花金「齒輪」增加健康和傷害值

「我是鐵匠等等等等等」

  1. 升級武器(傷害提高)
  2. 升級護甲(增加生命)
  3. 離開鎮(返回球員返回主菜單),當玩家死亡或選擇退出遊戲

遊戲結束

顯示比分並感謝玩家玩遊戲

以下僅僅是邏輯原型

我覺得這個應該可以工作,但是每次運行它時, 它都不能正常工作,只是最終會做一個無限循環的 。我希望你們中的一個能夠 明白爲什麼它不起作用,並且有點偏向我 的方向。任何事情將不勝感激!

還有關於那些陣營,地牢,城鎮布爾的任何評論? 我不知道我是否真的需要這些,它可能只是 額外的無用的代碼,但我真的不知道。

import java.util.Scanner; 


public class logic 
{ 
    public static void main(String[] args) 
    { 

    boolean running = true; 
    Scanner in = new Scanner(System.in); 
    System.out.println("This is a test"); 

    while(running) 
    { 
     boolean camp = true; 
     boolean dungeon = false; 
     boolean town = false; 
     String input = in.nextLine(); 
     while(camp) 
     { 
      System.out.println("what u do?"); 
      System.out.println("1. go dungeon"); 
      System.out.println("2. go town"); 
      System.out.println("3. quit"); 
      if (input.equals("1")) 
      { 
       dungeon = true; 
       while(dungeon) 
       { 
        System.out.println("you are in the dungeon"); 
        dungeon = false; 
        break; 

       } 



      } 
      else if (input.equals("2")) 
      { 
       dungeon = false; 
       town = true; 
       while(town) 
       { 
        System.out.println("you are in the town"); 
        town = false; 
        break; 

       } 


      } 
      else if (input.equals("3")) 
      { 
       break; 


      } 
      else 
      { 
       System.out.println("invalid command!"); 


      } 


     } 


    } 

    } 

} 
+0

您可能需要將false分配給陣營並在某處運行。 – JFPicard

+1

糾正我,如果即時通訊錯誤,但我不能做到地牢或城鎮內,因爲他們在營地和運行循環 –

+0

你可能想在選項3 – JFPicard

回答

0

就個人而言,我不會用這麼多while循環,而是把每一部分的邏輯到它自己的功能,那麼使用的if-else-IF/switch語句來決定哪些功能應該被調用,並且在執行此操作一個while循環,遺憾的是我無法測試這個,因爲我沒有得到一個Java環境,並且自從使用Java以來​​它已經有一段時間了。

+0

好吧,是的,我一定會考慮這樣做。我同意while循環是sl。的。就像我說的那樣,雖然製作函數/對象並調用它們對我來說仍然有點奇怪,而且我仍然在學習如何做到這一點。 –

+0

哦,是的,我只是使用命令提示符和記事本++的一切。如果你有時間來測試代碼,這是一個簡單的方法,我真的很感激它:) –

+0

因此,搞清楚你的​​建議並不像我第一次想到的那樣辛苦......而且我得到了一個工作原型下來....非常感謝! :D –

0

這是我想出的新代碼。我能夠弄清楚如何將所有不同的領域分解成他們自己的方法。這應該使遊戲世界的構建變得更容易:D

import java.util.Scanner; 


public class LogicTest 
{ 
    Scanner in = new Scanner(System.in); 

    public static void main(String[] args) 
    { 
     LogicTest game; 
     game = new LogicTest(); // a new instance of the public class folder has to be created in order to use all the other methods 

     game.run(); 


    } 

    public void run() // this method is used to run the game from the main method 
     { 
      camp(); 
     } 

    public void quest() 
    { 
     System.out.println("you go on quest"); 
     camp(); 
    } 
    public void town() 
    { 
     System.out.println("you go to town"); 
     camp(); 
    } 
    public void camp() // this method acts as the game hub where the player can choose what they want to do next 
    { 
     System.out.println("you are in your camp"); 
     System.out.println("----------------------"); 
     System.out.println("what do you want to do?"); 
     System.out.println("1. go quest"); 
     System.out.println("2. go town"); 
     System.out.println("3. quit"); 


     String input = in.nextLine(); 
     // each one of these options just calls its respective method 
            // the methods are what have the menus and everything for each location 
     if (input.equals("1")) 
     { 
      quest(); 
     } 
     else if (input.equals("2")) 
     { 
      town(); 
     } 
     else if(input.equals("3")) // by leaving this empty the program has nothing else to read with this option and so it closes 
     { 

     } 
     else 
     { 
      camp(); 
     } 
    } 



}