2.2
/* 输入半径,计算圆的面积 */
import java.util.Scanner;
public class Javaeg0202 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter a number for radius: ");
double radius = input.nextDouble();
double area = radius * radius * Math.PI;
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
2.3
/* 输入三个数字,计算其平均值 */
import java.util.Scanner;
public class Javaeg0203 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter three numbers: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double average = (number1 + number2 + number3) / 3;
System.out.println("The average of " + number1 + " " + number2
+ " " + number3 + " " + " is " + average );
}
}
2.7 显示GMT时间
public class Javaeg0207 {
public static void main(String[] args) {
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds / 1000;
long currentSecond = totalSeconds % 60;
long currentMinutes = totalSeconds / 60;
long currentMinute = currentMinutes % 60;
long totalHours = currentMinutes / 60;
long currentHour = totalHours % 24;
System.out.println("Current time is " + currentHour + ":"
+ currentMinute + ":" + currentSecond + "GMT");
}
}
2.9
import java.util.Scanner;
public class Javaeg0209 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter annual interest rate, e.g., 7.25%: ");
double annualInterestRate = input.nextDouble();
double monthlyInterestRate = annualInterestRate / 1200;
System.out.print("Enter number of years as an integer, e.g., 5: ");
int numberOfYears = input.nextInt();
System.out.print("Enter loan amount, e.g., 120000.95: ");
double loanAmount = input.nextDouble();
double monthlyPament = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1+monthlyInterestRate, numberOfYears*12));
double totalPayment = monthlyPament * numberOfYears * 12;
System.out.println("The monthly payment is $" + (int)(monthlyPament*100)/100.0);
System.out.println("The total payment is $" + (int)(totalPayment*100)/100.0);
}
}