0

I have this program that displays the cost per gallon of gasoline.

System.out.printf("%10s %.2f %s %n", "At", cost , "per gallon,");

The output displays

At 5.12 per gallon,

but I want it to display

At $5.12 per gallon,

I have tried to add the $ right after "At" but it displays a space before showing the cost. Is there a way to remove the space between "$

4

3 回答 3

5

一个更简单的方法就是

System.out.printf("At $%.2f per gallon,%n", cost);

不需要所有这些%ss 和字符串文字,只需将字符串直接放入即可。

于 2013-09-14T22:06:27.430 回答
1

你可以很容易地做到这一点,而不需要使用很多这样的变量:

System.out.printf("At $%.2f per gallon,", cost);

如果你仍然想要变量,那么试试这个:

System.out.printf("%10s $%.2f %s %n", "At", cost , "per gallon,");
于 2013-09-14T22:06:59.270 回答
0
System.out.printf("%10s $%.2f %s %n", "At", cost , "per gallon,");
于 2013-09-14T22:05:23.203 回答