Java从入门到放弃篇10(Java习题2)

今天,接着跟大家分享一波Java编程。

首先,创建一个名为tm的java文件。接着,将下面的代码放进文件中。
代码如下图:

import java.util.Scanner;

/**
 * Finish_time:2021/3/21
 * @author Mr.Pan_学狂
 * Java Practice
 */
 
class tm{
    
    	
	public static void main(String[] args) {
    
    //主函数(主方法)
		System.out.println("Java使我快乐!!");
		test();//调用函数
	    test2();
		test3();
		test4();
		test5();
	}
	
	public static void test() {
    
    
		Scanner input = new Scanner(System.in);//创建输入对象
		System.out.print("请输入华氏度:");//不换行输入
		double HSD = input.nextDouble();//获取键盘的双精度浮点数
		double SSD = (5.0 / 9) * (HSD - 32);//华氏度转换摄氏度
		System.out.println("华氏度: "+HSD+"\n摄氏度:"+SSD);
		System.out.println();
	}
	
	public static void test2() {
    
    
		long totalMilliseconds = System.currentTimeMillis();//时间的表达格式为当前计算机时间和GMT时间
		//(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数
		//System.out.println(totalMillseconds);
		long totalSeconds = totalMilliseconds / 1000;//总秒数=总毫秒数/1000
		long currentSecond = totalSeconds % 60;//当前秒=总秒数%60
		long totalMinutes = totalSeconds / 60;//总分钟数=总秒数/60
		long currentMinute = totalMinutes % 60;//当前分钟=总分钟数%60
		long totalHours = totalMinutes / 60;//总小时数=总分钟数/60
		long currentHour = totalHours % 24;//当前小时=总小时数%24
		System.out.println("格林威治当前时间为"+currentHour+":"+currentMinute+":"+currentSecond);
		System.out.print("\n");
	}
	
	public static void test3() {
    
    
		Scanner input = new Scanner(System.in);//创建输入对象
		System.out.print("请输入采购金额:");//当行输入
		double purchaseAmount = input.nextDouble();//获取用户的键盘输入
		double tax = purchaseAmount * 0.06;//计算采购金额与销售税的关系
		System.out.println("销售税是"+(int)(tax * 100) / 100.0+"元");//使用int强制将double双精度浮点型转换成int整形。
		System.out.println();
	}
	
	public static void test4() {
    
    
		Scanner input = new Scanner(System.in);//创建输入对象
		System.out.print("请输入年利率:");
		double NLLV = input.nextDouble();//年利率
		double  YLLV = NLLV / 1200;//月利率
		System.out.print("请输入年数:");//不换行输入
		int number_years = input.nextInt();//获取键盘输入的年数,整形int
		System.out.print("请输入贷款金额:");
		double dk_money = input.nextDouble();//获取键盘输入的贷款金额,双精度浮点型double
		double monthlypayment = dk_money * YLLV / (1 - 1 / Math.pow(1+YLLV, number_years * 12));//计算贷款
		//与月付,年数,年利率的关系
		double totalPayment = monthlypayment * number_years * 12;//得出总计支付的钱
		
		System.out.println("每月应付款为"+(int)(monthlypayment * 100) / 100.0+"元");
		System.out.println("总计支付"+(int)(totalPayment * 100) / 100.0+"元");
		System.out.print("\n");
	}
	
	public static void test5() {
    
    
		Scanner input = new Scanner(System.in);//创建输入对象
		System.out.print("请输入一个整数的秒钟数:");//使得输入内容不换行
		int seconds = input.nextInt();//获取键盘输入的整数
		int  minutes = seconds / 60;//整数除法,结果仍然是整数,获取分钟数的部分
		int Seconds_part = seconds % 60;//获取秒钟数的部分
		System.out.println(seconds+"秒是"+minutes+"分钟和"+Seconds_part+"秒");
	}
}

运行结果,如下图:
在这里插入图片描述
最后,感谢大家前来观看鄙人的文章,文中或有诸多不妥之处,还望指出和海涵。

猜你喜欢

转载自blog.csdn.net/weixin_43408020/article/details/115059557