フォーマットコンソール出力
あなたは使用することができるSystem.out.printf
方法フォーマットされた出力がコンソールに表示しました
public class test {
public static void main(String[] args) {
double amount = 12618.98;
double interestRate = 0.0013;
double interest = amount * interestRate;
System.out.printf("Interest is $%4.2f", interest);
}
}
表示:利息は$ 16.40です
注:%が欠落していない、ドメイン幅は4であり、精度は2であり、fはフォーマット識別子であります
System.out.printf(format, item1, item2, ......, itmek);
共通フォーマット識別子
識別子 | 輸出 | 例えば |
---|---|---|
%bの | ブール値 | trueまたはfalse |
%のC | 文字 | 'A' |
%dの | 10進整数 | 200 |
%F | フロート | 46.460000 |
%eは | 標準的な科学的表記数の形で | 4.556000e + 01 |
%sの | 弦 | 「Javaは楽しいです」 |
デフォルトでは、小数点以下の表示後の浮動小数点値で6
System.out.printf("%8d%8s%8.1f\n", 1234,"Java", 5.63);
System.out.printf("%-8d%-8s%-8.1f \n", 1234, "Java", 5.63);
FormatDemo.java
public class test {
public static void main(String[] args) {
System.out.printf("%-10s%-10s%-10s%-10s%-10s\n", "Degree", "Radians", "Sine",
"Cosine", "Tangent");
int degrees = 30;
double randians = Math.toRadians(degrees);
System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f", degrees, randians, Math.sin(randians),
Math.cos(randians), Math.tan(randians));
}
}
第4章プログラミング演習
4.2 几何:最大圆距离
import java.util.Scanner;
public class CircleDistansce {
public static void main(String[] args) {
System.out.print("Enter point 1 (latitude and longitude) in degrees:");
Scanner input = new Scanner(System.in);
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter point 2 (latitude and longitude) in degrees:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double earthRadius = 6371.01; // 记得转换为弧度制 Math.toRadians()
double d = earthRadius * Math.acos(Math.sin(Math.toRadians(x1)) * Math.sin(Math.toRadians(x2)) +
Math.cos(Math.toRadians(x1)) * Math.cos(Math.toRadians(x2)) * Math.cos(Math.toRadians(y2 - y1)));
System.out.print("The distance between the 2 points is:" + d);
}
}
度の点1(緯度及び経度)を入力:39.55 -116.25
度のポイント2(緯度及び経度)を入力:41.5 87.37
2点間の距離である。10691.79183231593
4.2 圆上面的随机点
import java.util.Scanner;
public class test {
public static void main(String[] args) {
final double r = 40;
double alpha1 = Math.random() * 400;
double x1 = r * Math.cos(Math.toRadians(alpha1));
double y1 = r * Math.sin(Math.toRadians(alpha1));
double alpha2 = Math.random() * 400;
double x2 = r * Math.cos(Math.toRadians(alpha2));
double y2 = r * Math.sin(Math.toRadians(alpha2));
double alpha3 = Math.random() * 400;
double x3 = r * Math.cos(Math.toRadians(alpha3));
double y3 = r * Math.sin(Math.toRadians(alpha3));
double line1 = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
double line2 = Math.sqrt(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2));
double line3 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
double angleA = Math.toDegrees(Math.acos((line1 * line1 - line2 * line2 - line3 * line3) / (-2 * line2 * line3)));
double angleB = Math.toDegrees(Math.acos((line2 * line2 - line1 * line1 - line3 * line3) / (-2 * line3 * line1)));
double angleC = Math.toDegrees(Math.acos((line3 * line3 - line2 * line2 - line1 * line1) / (-2 * line2 * line1)));
System.out.printf("3个角度分别为:%10.2f%10.2f%10.2f", Math.round(angleA * 100) / 100.0,
Math.round(angleB * 100) / 100.0, Math.round(angleC * 100) / 100.0);
}
}
3つの角度は、以下のとおりです。129.50 15.14 35.36
Gqqによる書き込み