2014-01-24 64 views
3

我正在嘗試創建基於文本的冒險遊戲,並且我正在嘗試添加一項功能,只要遊戲需要您的輸入,標記,「=>」將顯示在您輸入的位置旁邊。有了這裏的代碼,無論何時我在命令提示符(Windows 7)中運行它,輸入都會顯示,而小勾號則不會。在你輸入之後,輸入後就會顯示勾號。我希望能夠像這樣在一行上標記刻度和輸入,「=> [使用輸入]」。請幫忙!爲什麼打印聲明「=>」在您輸入掃描儀後輸入

import java.io.IOException; 
import java.util.Scanner; 

import org.fusesource.jansi.AnsiConsole; 



public class Game { 

    public static String BLACK = "\u001B[0;30m"; 
    public static String RED = "\u001B[0;31m"; 
    public static String GREEN = "\u001B[0;32m"; 
    public static String YELLOW = "\u001B[0;33m"; 
    public static String BLUE = "\u001B[0;34m"; 
    public static String MAGENTA = "\u001B[0;35m"; 
    public static String CYAN = "\u001B[0;36m"; 
    public static String WHITE = "\u001B[0;37m"; 

    public final static String ESC = "\033["; 

    public static String name; 
    public static String gender; 

    public static void main(String[] args) { 
     AnsiConsole.systemInstall(); 

     check(); 
    } 

    public static void start() { 

     Scanner a = new Scanner(System.in); 

     System.out.println("###############################################################################"); 
     System.out.println("#                    #"); 
     System.out.println("#      " + YELLOW + "^.^" + WHITE + " " + "Adventure Time Is Yes " + YELLOW + "^.^" + WHITE + "      #"); 
     System.out.println("#                    #"); 
     System.out.println("###############################################################################"); 

     //Get Name 
     System.out.print("\n"); 
     System.out.println("Hello young lad! What is thy " + RED + "name " + WHITE + "you were given at birth?"); 
     System.out.print("=> "); 
     //Global Variable for Name 
     name = a.next(); 

     System.out.print("\n"); 

     System.out.println("Hello " + RED + name + WHITE + "! Are you a Male or Female?"); 
     System.out.print("=> "); 
     gender = a.next(); 


    } 

    public static void check() { 

     Scanner a = new Scanner(System.in); 

     System.out.println("Can you see these colors?: "); 
     System.out.println(RED + "Red " + GREEN + "Green " + YELLOW + "Yellow " + BLUE + "Blue " + MAGENTA + "Magenta " + CYAN + "Cyan " + WHITE + "White"); 
     System.out.println("Y/N"); 
     System.out.print("=> "); 

     String as = a.nextLine(); 
     as.toUpperCase(); 

     if (as == "Y") { 
      System.out.println(ESC + "2J"); 
      start(); 
     } else { 
      System.out.println(ESC + "2J"); 
      start(); 
      //Future idea is to make it so there is an exact copy of the game but without color :D 
     } 

    } 
} 

/*try { 
System.in.read(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 

System.exit(0);*/ 

//System.out.println(ESC + "2J"); Clear Lines On Screen 

回答

1

在詢問輸入之前,刷新輸出流,以確保寫入的內容都已完全通過緩衝區。

+1

我該如何去沖洗輸出流? – uwjklwme23584729

2

您可以使用outputstreamName.flush()刷新輸出流。欲瞭解更多幫助,請參閱this exampledocs

刷新輸出流將幫助您找出代碼失敗的位置。

+0

我不是試圖打印到文件,我試圖打印到控制檯。所以通過使用outputstreamName.flush()將不可用是否正確? – uwjklwme23584729

+0

錯誤。可以針對任何緩衝的輸出流調用刷新操作,並且應該在您希望確保緩衝區已經完全處理到輸出時調用該操作。你的問題是爲什麼人們可能想要這樣做的一個完美例子。 – keshlam