2020 |
07,17 |
/*
printf とは。
printf()メソッドを使うことで書式を指定して文字列を表示することができる
*/
public class MyApp {
public static void main(String[] args) {
int score = 50;
double height = 165.8;
String name = "taguchi";
//文字列の場合は%s、整数値の場合は%d、浮動小数点は%fを使用する
System.out.printf("name: %s, score : %d, height: %f\n",name,score,height );
//名前とスコアに関して10桁表示するようにするに%-10sと入力する
//%-10s(左詰め)、%10s(右詰め)
//整数部分を5桁、小数点以下を2桁にしたい場合は%5.2fと入力
System.out.printf("name: %-10s, score : %10d, height: %5.2f\n",name,score,height );
//文字列として受け取りたい場合はStringのformatメソッドを使うこと
String s = String.format("name: %10s, score : %10d, height: %5.2f\n",name,score,height );
System.out.println(s);
}
}

PR
Post your Comment