2013-10-17 16 views

回答

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); 

BTW,而不是\n您可以使用%n生成操作系統特定的一組分隔符,例如用於Windows的\r\n。它會產生與System.lineSeparator()相同的結果。

0

爲什麼不做這樣的事情?

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

這應該怎麼辦?

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

或者你的意思是不同的?

0

試試這個

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