Formatting console output
You may be used System.out.printf
a method formatted output displayed on the console
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);
}
}
Display: Interest is $ 16.40
Note:% is not missing, the domain width is 4, the precision is 2, f is format identifier
System.out.printf(format, item1, item2, ......, itmek);
Common format identifier
Identifier | Export | For example |
---|---|---|
%b | Boolean value | true or false |
%c | character | ‘a' |
%d | Decimal integer | 200 |
%f | Float | 46.460000 |
%e | In the form of standard scientific notation number | 4.556000e+01 |
%s | String | “Java is fun" |
By default, a floating point value after the decimal point display 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));
}
}
Chapter 4 Programming Exercises
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);
}
}
Enter point 1 (latitude and longitude) in degrees: 39.55 -116.25
Enter point 2 (latitude and longitude) in degrees: 41.5 87.37
The distance between the 2 points is: 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);
}
}
Three angles are: 129.50 15.14 35.36
Write by Gqq