14
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Java】標準出力に色をつける

Last updated at Posted at 2019-04-07

変数iが0-7へ変化してるのでそこに注意してください

Color.java
public class Color{
    public static void main(String[] args){
        //文字色を変える
        for(int i=0;i<8;i++){
            //int i =0;
	    System.out.println("\u001b[00;3"+i+"m esc[00;3"+i+" \u001b[00m");
        }
        //背景色を変える
        for(int i=0;i<8;i++){
	    System.out.println("\u001b[00;4"+i+"m esc[00;4"+i+" \u001b[00m");	
        }
    }
}

出力結果
スクリーンショット 2019-04-07 20.44.29.png

こんなかんじで色付けできます

また,頻繁に使うって人はこんなかんじで先にストック作っておくのもいいと思います

Color.java
public class Color{
    public static void main(String[] args){
        String red    = "\u001b[00;31m";
        String green  = "\u001b[00;32m";
        String yellow = "\u001b[00;33m";
        String purple = "\u001b[00;34m";
        String pink   = "\u001b[00;35m";
        String cyan   = "\u001b[00;36m";   
        String end    = "\u001b[00m";
        
        String[] names = new String[]{red,green,yellow,purple,pink,cyan};
        for(int i=0; i< names.length; i++){        
	    System.out.println(names[i]+"hello"+end);
        }
    }
}

コマンドライン
スクリーンショット 2019-04-07 21.20.01.png

14
11
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?