【Java程序设计】第二章例题

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);
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值