標題:
[作業] printf() 函式 (一)
[打印本頁]
作者:
tonyh
時間:
2019-2-18 21:01
標題:
[作業] printf() 函式 (一)
本帖最後由 tonyh 於 2019-2-25 19:24 編輯
假設本練習中的四個變數分別為
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
試利用printf()函式完成如下之執行畫面:
[attach]5968[/attach]
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("%09.2f%n",c); //指定總長度為9個字元,不足的部分以 0 補上
System.out.printf("%09.2f%n",d);
}
}
複製代碼
作者:
黃宇綸
時間:
2019-2-19 23:14
public class Ch01
{
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);
}
}
複製代碼
作者:
洪翊庭
時間:
2019-2-24 17:58
本帖最後由 洪翊庭 於 2019-2-25 19:23 編輯
public class Ch02
{
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("%09.2f%n",c);
System.out.printf("%09.2f%n",d);
}
}
複製代碼
作者:
洪翊展
時間:
2019-2-24 18:05
本帖最後由 洪翊展 於 2019-2-25 19:21 編輯
public class Ch01
{
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("%09.2f%n",a);
System.out.printf("%09.2f%n",b);
System.out.printf("%09.2f%n",c);
System.out.printf("%09.2f%n",d);
}
複製代碼
作者:
黃宇瑄
時間:
2019-2-25 18:11
public class Ch01
{
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);
}
}
複製代碼
作者:
黃宇綸
時間:
2019-2-25 19:21
public class Ch01
{
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("%09.2f%n",c);
System.out.printf("%09.2f%n",d);
}
}
複製代碼
作者:
李沛昂
時間:
2019-2-25 19:22
public class Pig
{
public static void main(String[] args)
{
double a=3.456789;
double b=34.56789;
double c=345.6789;
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);
}
}
複製代碼
豬事大吉
作者:
孫焌仁
時間:
2019-2-25 19:24
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("%09.2f%n",c);
System.out.printf("%09.2f%n",d);
}
}
複製代碼
作者:
戴嘉禾
時間:
2019-2-25 19:24
public class Ch100
{
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);
}
}
複製代碼
作者:
趙一鳴
時間:
2019-2-25 19:25
public class Ch01
{
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("%09.2f%n",c);
System.out.printf("%09.2f%n",d);
}
}
複製代碼
作者:
曾堂桂
時間:
2019-2-25 19:25
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);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2