標題:
printf() 函式 (一)
[打印本頁]
作者:
tonyh
時間:
2015-3-14 17:33
標題:
printf() 函式 (一)
本帖最後由 tonyh 於 2015-3-28 16:29 編輯
假設本練習中的四個變數分別為
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
試利用printf()函式完成如下之執行畫面:
[attach]1173[/attach]
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); //四捨五入至小數點後第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);
}
}
複製代碼
作者:
李允軒
時間:
2015-3-14 17:48
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);
}
}
複製代碼
作者:
劉得恩
時間:
2015-3-14 17:49
import java.util.Scanner;
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);
}
}
複製代碼
作者:
林宇翔
時間:
2015-3-14 17:49
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);
}
}
複製代碼
作者:
張峻瑋
時間:
2015-3-14 17:51
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);
}
}
複製代碼
作者:
周雍程
時間:
2015-3-14 17:53
import java.util.Scanner;
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);
}
}
複製代碼
作者:
張彥承
時間:
2015-3-14 17:53
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); //四捨五入至小數點後第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);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2