package com.company; import java.util.Scanner; public class Chapter2_19 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Enter three points for a triangle: "); double x1 = input.nextDouble(); double x2 = input.nextDouble(); double x3 = input.nextDouble(); double y1 = input.nextDouble(); double y2 = input.nextDouble(); double y3 = input.nextDouble(); double b1 = Math.pow((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2),0.5); double b2 = Math.pow((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3),0.5); double b3 = Math.pow((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1),0.5); double s = (b1 + b2 + b3) / 2; double area = Math.pow(s * (s - b1) * (s - b2) * (s - b3),0.5); System.out.println("The area of the triangle is " + area); } }
/Users/wanshuai/Library/Java/JavaVirtualMachines/openjdk-15.0.1/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=49335:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/wanshuai/IdeaProjects/untitled/out/production/untitled com.company.Chapter2_19
Enter three points for a triangle:
1.5 -3.4 4.6 5 9.5 -3.4
The area of the triangle is 13.605000000000025
Process finished with exit code 0
书上的答案是33.6
package com.company; import java.util.Scanner; public class Chapter2_20 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Enter balance and interest rate (e.g., 3 for 3%): "); double balance = input.nextDouble(); double interestRate = input.nextDouble(); double interest = (int)(balance * (interestRate / 1200) * 100000) / 100000.0; System.out.println("The interest is " + interest); } }
/Users/wanshuai/Library/Java/JavaVirtualMachines/openjdk-15.0.1/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=49533:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/wanshuai/IdeaProjects/untitled/out/production/untitled com.company.Chapter2_20
Enter balance and interest rate (e.g., 3 for 3%):
1000 3.5
The interest is 2.91666
Process finished with exit code 0