2015-07-20 17 views
1

如何實現從鍵盤讀取的數據,而無需將光標移動到使用Java的下一行控制檯?從鍵盤讀取Windows命令行的數據,而不將光標移動到下一行

具體來說就是命令行窗口。

java.util.Scanner將光標移動到其下一行的方法,就像java.io.Console一樣。

據我所知,將遊標返回到Java控制檯中的上一行不能。

public static String Read() { 
    Scanner Sc = new Scanner(System.in); 
    return Sc.next(); 
} 

System.out.print("Turn of player: "); 
TempPlayerGuess = Integer.parseInt(Read()); 
System.out.print("."); 

而且我看到:玩家

轉到:4

但我希望看到:玩家

轉到:4

+0

你能提供你正在嘗試閱讀的示例輸入嗎? – Burusothman

+0

@Exbury 我編輯過。 – Destructor

回答

0

我不認爲這是可能的。 System.in是用於訪問標準輸入的InputStream。輸入流不會在用戶按下返回鍵之前提供數據(可能是平臺特定的)。由於所有標準輸入都使用System.in,我不認爲有可能做你想問的問題。這是一個例子。

import java.io.IOException; 

public class ReadInput { 
    public static void main(String[] args) throws IOException { 
     int readByte = -1; 
     while ((readByte = System.in.read()) != -1) { 
      System.out.println(String.format("Read character code %d", readByte)); 
     } 
    } 
} 
+0

是否可以在InputStream中傳遞字符以某種方式發生,如上所示? @Samuel – Destructor

+0

我沒有這方面的經驗,但有假設它的庫。看到這個問題http://stackoverflow.com/questions/1001335/java-gotoxyx-y-for-console-applications – Samuel

相關問題