2013-07-31 36 views
-4

這是我第一次來這裏,我對編程非常陌生,我一直在學習java 2周,現在我只知道一些基本知識,所以我決定測試我的通過做一個簡單的文本冒險遊戲,而不使用對象的知識,因爲我還沒有掌握。我將繼續打印故事並描述情況,並接受玩家選擇繼續。 但我有我的代碼的問題,我不知道如何重複一個問題,如果用戶輸入一個無效的選擇,我嘗試了幾種方法,但程序結束或給我無限循環,我需要一些幫助,請。Java - 創建沒有任何對象的文本冒險,

這裏是我的代碼:

import java.util.Scanner; 

class The_Crime { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Welcome Stranger, Please Enter Your Name"); 
     String name = input.nextLine(); 
     System.out.println("Welcome" + name);  
     Street street = new Street(); 

     System.out.println("You are now in the street with a dead body on the floor drowned in blood \n there is a building right ahead of you\n What would you like to do?"); 
     String choice = input.nextLine(); 
     while(true) { 
      if(choice.equals("enter building")) { 
       System.out.println("You have entered the empty building");  
      } else { 
       System.out.println("You are now in the street with a dead body lying around drawned in blood \n there is a building right infront of you\n What would you like to do?"); 
      } 
     }  
    } 
} 
+7

你一定要明白'System','Scanner','String','Street'都是對象 – chancea

+5

它是100%不可能不使用對象在java – aaronman

+2

heyy只是使用原始類型!你在自己的gameeeee中作弊! – nachokk

回答

1

你而病情會永遠持續下去你的,因爲從來沒有break了它。但你並不需要做這種情況下,只需更改

while(true) 
    if (choice.equals("enter building")) 
      System.out.println("You have entered the empty building"); 

    else 
      System.out.println("You are now in the street with a dead body lying around drawned in blood \n there is a building right infront of you\n What would you like to do?"); 

while(choice.equals("some invalid choice")) 
{ 
    choice = input.nextLine(); 
} 

while(!choice.equals("some valid choice")) 
{ 
    choice = input.nextLine(); 
} 
+0

這並沒有退出解決方案,但它讓我想出了它。 – Abdou23

+0

@ user2628367我很高興它爲你工作:)感謝您接受 – chancea

0
while (true) { 
    System.out.println("You are now..."); 
    String choice = Scanner.nextLine(); 
    if (choice.equals("enter building")) break; 
} 
0

的問題是,你永遠在循環中改變選擇的值。基本上,一旦他們做出選擇,他們必須永遠做出選擇。

choice = input.nextLine(); 

加上你的循環將無限期地發生,因爲它總是如此。你可能爲了退出循環或者有一個變量來決定你的狀態而中斷。

int state = 1; 
while(state == 1) { 
    choice = input.nextLine(); 
    if(choice.equals("leave")) { 
     state = 2; 
     System.out.println("Goodbye"); 
    } 
} 

每個好的遊戲都有狀態和某種有限狀態機。

1

在這種情況下,do-while循環我會建議使用,在這裏你有第一次的問題要問,後來決定是基於選擇

Scanner input = new Scanner(System.in); 
String choice=null; 

do{ 

System.out.println("What would you like to do?"); 
choice = input.nextLine(); 


}while(!choice.equals("enter building")); 

我不知道你的具體的要求,所以混亂的條件根據需要,但我認爲應該做的,而在這裏。

在你的情況下,沒有爲while(true){ //Infinite loop }提到的破壞條件,這就是爲什麼它無限。

0

更換

 String choice = input.nextLine(); 
    while(true) 
     if (choice.equals("enter building")) 
      System.out.println("You have entered the empty building"); 

     else 
      System.out.println("You are now in the street with a dead body lying around drawned in blood \n there is a building right infront of you\n What would you like to do?"); 

while(!input.nextLine().equals("enter building")){ 
     System.out.println("You are now in the street with a dead body on the floor drowned in blood \n there is a building right ahead of you\n What would you like to do?"); 
    }