末尾の文字列 "(ポンド)" を表示したいのですが、私のスキャナ クラスではそれらが省略され続けています。なぜこれが起こっているのか教えてください。
System.out.printf("Weight: %.1f \n", person.getWeight(), " (pounds)");
System.out.printf("Height: %.1f \n", person.getHeight(), " (inches)");
末尾の文字列 "(ポンド)" を表示したいのですが、私のスキャナ クラスではそれらが省略され続けています。なぜこれが起こっているのか教えてください。
System.out.printf("Weight: %.1f \n", person.getWeight(), " (pounds)");
System.out.printf("Height: %.1f \n", person.getHeight(), " (inches)");
%s
ユニットを含む文字列を次のように配置する場所をフォーマットで表示する必要があります
System.out.printf("Weight: %.1f %s\n", person.getWeight(), "(pounds)");
System.out.printf("Height: %.1f %s\n", person.getHeight(), "(inches)");
または、ユニットをフォーマットに手動で配置することもできます
System.out.printf("Weight: %.1f (pounds)\n", 1.1);
System.out.printf("Height: %.1f (inches)\n", .2);
ところで、代わりにWindowsのような OS 固有の区切り記号のセットを作成するために\n
使用できます。と同じ結果になります。%n
\r\n
System.lineSeparator()
なぜこのようなことをしないのですか?
System.out.printf("Weight: %.1f (pounds)\n", person.getWeight());
System.out.printf("Height: %.1f (inches)\n", person.getHeight());
これはそれを行う必要がありますか?
System.out.printf("Weight: %.1f (pounds)\n", person.getWeight(), );
それとも違う意味ですか?
これを試して
System.out.printf("Weight: %.1f (pounds) \n", person.getWeight());