2014-10-27 41 views
-2

我是這個論壇的新手,並且一般編碼。我決定自學Java作爲我的第一種編程語言。我有一天訂購了一本名爲「Head First Java」的書,希望當我得到它時,我將能夠更有效地教授自己。無論如何,我認爲我會嘗試創建一個遊戲來幫助我學習Java(因爲我的目標是學習編程以構建自己的視頻遊戲)。我今天開始了一個文本冒險遊戲。Java文本冒險 - 你如何爲用戶做出選擇?

這是我到目前爲止有:

import java.util.Scanner; 
public class Main { 

    public static void main (String[] args){ 

    // Introduction 
    System.out.println("The year is 2507..."); 
    System.out.println(""); 
    System.out.println("Years have passed since the war."); 
    System.out.println("The people lucky enough to survive the invasion migrated\nto the newly discovered planet Rothgar in an attempt to start a new life."); 
    System.out.println("Will you survive on this new world, or will the human race fall into extinction? \n"); 

    Scanner choice = new Scanner(System.in); 
    Scanner keyIn = new Scanner(System.in); 
    System.out.print("Press Enter to continue.."); 
    keyIn.nextLine(); 

    // Main Menu 
    System.out.println("            Main Menu"); 
    System.out.println("----------------------------------------------------------------------------------------------------------------------------------"); 
    System.out.println("Enter one of the following options:\n"); 
    System.out.println("'Start' to begin.\n"); 
    System.out.println("'Exit' to quit the game.\n"); 
    System.out.println("'Instructions' to learn how to play.\n"); 
    System.out.println("'Credits' to learn more about the game.\n"); 


    } 



} 

我很抱歉,如果我的編碼是醜陋與否儘可能有效,但就像我說的,我試圖教我。現在,我的問題是:我如何分解這四個選項?當用戶輸入其中一個時,我希望我的程序轉到代碼的相應部分(我還沒有寫入)。例如,如果用戶輸入「Credits」,我希望輸出成爲我遊戲的點數,然後我需要程序返回到主菜單,可能在用戶再次按下「Enter」後。如果玩家沒有從主菜單中輸入這四個選項中的任何一個,我希望我的程序說「你沒有很好地遵循方向,是嗎?讓我們再試一次。」

我希望我明確地問我的問題。所有幫助表示讚賞!我剛剛開始學習編程Java,我顯然還是一個新手,但我打算最終作爲獨立遊戲開發者發佈自己的遊戲! :)

+2

繼續閱讀你的書,你會學到答案。這實際上是一個非常廣泛的「如何編寫程序」的問題。 – John3136 2014-10-27 01:55:19

+0

您將不得不更多地瞭解方法,類,OOP(面向對象編程)和對象,從本質上轉向下一級編程來解決這個問題。繼續閱讀你的書和/或教程,並不要放棄。 – 2014-10-27 01:55:41

回答