簡體   English   中英

更改字符串的輸入顏色

[英]Change the input color of string

我想將輸入字符串的顏色更改為綠色。 用戶鍵入時,它將呈綠色。 我不想先輸入字符串然后更改顏色。 誰能告訴如何做到這一點?

System.out.print("Please enter a sentence: ");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();   // I want to change the color to green when user typing the string

輸出:

您可以使用ANSI 轉義符以與更改輸出顏色相同的方式更改輸入顏色。 這是一篇特定於 Java 的帖子。

訣竅是在讀取用戶輸入之前不要將其重置為正常( \ )。 例如:

System.out.print("Please enter a sentence: \u001b[92m"); // Note the \u001b[92m after the prompt
Scanner s = new Scanner(System.in);
String name = s.nextLine();
System.out.println("\u001b[0mOther output"); // note the reset \u001b[0m

這可能不適用於您的 IDE 控制台,因為某些 IDE 將單獨的顏色(您可以在設置中設置)應用於所有控制台輸入。 所以你應該在你的計算機的命令行中運行它(例如cmd 、 Terminal.app 等)。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM