【java】第2章 基本程序设计 测试题(作业部分)

文章目录

2.7

在这里插入图片描述

//2.7
import java.util.Scanner;
public class Demo {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number of minutes: ");
		long minutes, years, days, allDays;
		minutes = input.nextLong();
		allDays = minutes / (60 * 24);
		years = allDays / 365;
		days = allDays % 365;
		System.out.print(minutes + " minutes is approximately "
				+ years + " years and " + days + " days ");
	}

}

2.8

在这里插入图片描述

//2.8 (2-7改)
import java.util.Scanner;
public class Demo {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the time zone offset to GMT: ");
		long zoneOffset = input.nextInt();
		long totalMilliSeconds = System.currentTimeMillis();
		long totalSeconds = totalMilliSeconds / 1000;
		long currentSecond = totalSeconds % 60;
		long totalMinutes = totalSeconds / 60;
		long currentMinute = totalMinutes % 60;
		long totalHours = totalMinutes / 60;
		long currentHour = totalHours % 24;
		currentHour += zoneOffset;
		System.out.print("The current time is " + currentHour 
				+ ":" + currentMinute + ":" + currentSecond);
	}

}

2.17

在这里插入图片描述

//2.17
import java.util.Scanner;
public class Demo {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		double ta, v, twc, tmp;
		System.out.println("Enter the temperature in Fahrenheit between -58°F and 41°F: ");
		ta = input.nextDouble();
		System.out.print("Enter the wind speed (>=2) in mils per hour: ");
		v = input.nextDouble();
		tmp = Math.pow(v, 0.16);
		twc = 35.74 + 0.6215 * ta - 35.75 * tmp + 0.4275 * ta * tmp;
		System.out.println("The wind chill index is " + twc);
	}

}

2.22

在这里插入图片描述

//2.22 (2-10改)
import java.util.Scanner;
public class Demo {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter an amount in int, " + 
		"which the last two numbers mean cents (for exampel 1156 means 11.56) : ");
		int amount = input.nextInt();
		int remainingAmount = amount;
		int numberOfOneDollars = remainingAmount / 100;
		remainingAmount = remainingAmount % 100;
		int numberOfQuarters = remainingAmount / 25;
		remainingAmount = remainingAmount % 25;
		int numberOfDimes = remainingAmount / 10;
		remainingAmount = remainingAmount % 10;
		int numberOfNickels = remainingAmount / 5;
		remainingAmount = remainingAmount % 5;
		int numberOfPennies = remainingAmount;
		System.out.println("Your amount " + amount + " consist of");
		System.out.println("    " + numberOfOneDollars + " dollars");
		System.out.println("    " + numberOfQuarters + " quaters");
		System.out.println("    " + numberOfDimes + " dimes");
		System.out.println("    " + numberOfNickels + " nickels");
		System.out.println("    " + numberOfPennies + " pennies");
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_碗碗儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值