標題:
printf() 函式 (一)
[打印本頁]
作者:
tonyh
時間:
2016-7-29 19:41
標題:
printf() 函式 (一)
本帖最後由 tonyh 於 2017-5-6 12:00 編輯
假設本練習中的四個變數分別為
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
試利用printf()函式完成如下之執行畫面:
public class Ch64
{
public static void main(String[] args)
{
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
System.out.printf("%.3f%n",a); //四捨五入至小數點後第3位
System.out.printf("%.2f%n",b);
System.out.printf("%.1f%n",c);
System.out.printf("%.0f%n",d);
System.out.printf("%9.2f%n",a); //指定總長度為9個字元
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
作者:
沈子耕
時間:
2016-7-29 20:03
public class Ch64 {
public static void main(String[] args) {
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
System.out.printf("%.3f%n",a);
System.out.printf("%.2f%n",b);
System.out.printf("%.1f%n",c);
System.out.printf("%.0f%n",d);
System.out.printf("%9.2f%n",a);
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
作者:
梁和雋
時間:
2016-7-29 20:09
public class Ch64
{
public static void main(String[] args)
{
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
System.out.printf("%.3f%n",a);
System.out.printf("%.2f%n",b);
System.out.printf("%.1f%n",c);
System.out.printf("%.0f%n",d);
System.out.printf("%9.2f%n",a);
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
作者:
洪振庭
時間:
2016-7-29 20:15
本帖最後由 洪振庭 於 2016-7-29 20:30 編輯
public class Ch64
{
public static void main(String[] args)
{
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
System.out.printf("%.3f%n",a);
System.out.printf("%.2f%n",b);
System.out.printf("%.1f%n",c);
System.out.printf("%.0f%n",d);
System.out.printf("%9.2f%n",a);
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
作者:
黃璽安
時間:
2016-7-29 20:17
public class Ch68 {
public static void main(String[] args) {
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
System.out.printf("%.3f%n",a);
System.out.printf("%.2f%n",b);
System.out.printf("%.1f%n",c);
System.out.printf("%.0f%n",d);
System.out.printf("%9.2f%n",a);
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
作者:
曾挺桂
時間:
2016-7-29 20:17
public class Ch64 {
public static void main(String[] args) {
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
System.out.printf("%.3f",a);
System.out.printf("%.2f",b);
System.out.printf("%.1f",c);
System.out.printf("%.0f",d);
System.out.printf("%9.2f%n",a);
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
作者:
陳思惟
時間:
2016-7-29 20:17
public class Ch65 {
public static void main(String[] args) {
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
System.out.printf("%.3f%n",a);
System.out.printf("%.2f%n",b);
System.out.printf("%.1f%n",c);
System.out.printf("%.0f%n",d);
System.out.printf("%9.2f%n",a);
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
作者:
李知易
時間:
2016-7-29 20:17
public class Ch67 {
public static void main(String[] args)
{
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
System.out.printf("%.3f%n",a);
System.out.printf("%.2f%n",b);
System.out.printf("%.1f%n",c);
System.out.printf("%.0f%n",d);
System.out.printf("%9.2f%n",a);
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2