0

我希望显示末尾的字符串“(磅)”,但我的扫描仪类一直将它们排除在外。告诉我为什么会这样?

   System.out.printf("Weight: %.1f \n", person.getWeight(), " (pounds)");
   System.out.printf("Height: %.1f \n", person.getHeight(), " (inches)");
4

4 回答 4

2

您需要以您的格式显示%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);

顺便说一句,\n您可以使用%n生成操作系统特定的分隔符集,例如\r\nWindows。它将产生与 相同的结果System.lineSeparator()

于 2013-10-17T03:23:34.620 回答
0

为什么不做这样的事情?

System.out.printf("Weight: %.1f (pounds)\n", person.getWeight());
System.out.printf("Height: %.1f (inches)\n", person.getHeight());
于 2013-10-17T03:24:06.803 回答
0

这应该做吗?

System.out.printf("Weight: %.1f (pounds)\n", person.getWeight(), );

或者你的意思是不同的?

于 2013-10-17T03:24:11.867 回答
0

试试这个

 System.out.printf("Weight: %.1f (pounds) \n", person.getWeight());
于 2013-10-17T03:24:16.470 回答