JAVA语言程序设计课后习题----第二单元解析(仅供参考)
2024-09-04 01:47:58
1 注意不同类型转换
import java.util.Scanner; public class Ch02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double f = sc.nextDouble();
double t = (5.0/9)*(f-32); // 注意 (5/9) 结果为 整形 要写成 (5.0/9)
System.out.println(t)
}
2 本题水题,会圆柱体的基本公式即可
import java.util.Scanner; public class Ch02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入圆柱体半径和高:");
double r = sc.nextDouble();
double h = sc.nextDouble();
double v = Math.PI * r * r * h; //圆周率方法 Math.PI
System.out.println(v);
}
3 本题水题,知道公式即可
import java.util.Scanner; public class Ch02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入体重和身高:");
double g = sc.nextDouble();
double h = sc.nextDouble();
double BMI = g / (h * h);
System.out.println(BMI);
}
4 使用 循环 每次对 个位 取数相加 取数后 除以10 使前一位 变为 个位 继续 判断小于等于0时停止
import java.util.Scanner; public class Ch02 {
public static void main(String[] args) {
int sum = 0;
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
while (num1 > 0) // 与 0 比较 判断 是否 需要 取数
{
sum += (num1%10); // 通过取余 获得个位数字
num1 /= 10; // 每次取余后 除以10
}
System.out.println(sum);
}
5 注意 System.currentTimeMillis()方法 返回 long 形 需要转换为 int 形
public class Ch02 {
public static void main(String[] args) {
// 通过 System.currentTimeMillis() 方法 获得 从1970年1月1日 00:00:00 到现在的 毫秒数
// 28800000 是 格林时间 与 我们 时区 的 时间差值
// 对 86400000 取余 是 把 满一天的时间都去掉 获取多出来的不足一天的时间
int t = (int)((System.currentTimeMillis()+28800000)%86400000);
int hour = t/3600000; // 除 3600000 获取满小时的个数 即 求小时 为几点
int mine = t%3600000/60000; // 计算 不足一小时 的时间 里 有多少分钟
int s = t%3600000%60000/1000; // 计算不足一分钟的时间里 有多少秒 不要忘记 除以 1000 (因为 单位 为 毫秒)
System.out.println("当前时间: "+hour+":"+mine+":"+s+" GMT");
}
6 a 不能为 0 b2 - 4 * a * c 不能为 0
import java.util.Scanner; public class Ch02 {
public static void main(String[] args) {
double sum,a,b,c,t;
Scanner sc = new Scanner(System.in);
while (true) {
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
t = b*b-4*a*c;
if (a == 0) {
System.out.println("a 不能为 0,请重新测试 ^_^");
} else if (t < 0) {
System.out.println("b*b-4*a*c不能为0,请重新测试");
}
else
{
break;
}
}
sum = ((-b+Math.sqrt(t)/(2*a))); //( 2*a ) 注意加括号
System.out.println(sum);
7 注意计算公式先后顺序 多使用小括号
import java.util.Scanner; public class Ch02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double yrate = sc.nextDouble();
double amount = sc.nextDouble();
double year = sc.nextDouble();
double month = (amount * (yrate/12))/(1-(1/Math.pow(1+yrate/12,year*12)));
double sumamount = month*12*year;
System.out.println("月支付金额:"+month+"\n总偿还金额:"+sumamount);
}
注意:
本单元感谢
一曲、長歌 的得力配合
最新文章
- android studio...混淆打包全揭秘
- PMP--论文部分
- JAVA基础知识之JVM-——URLClassLoader
- 超强vim配置
- JavaScript算法题之–随机数的生成
- C# 操作XML 如果不存在创建 存在直接追加
- 打开一个已经写好的Android studio工程的方法
- WebService开发步骤
- Java生成缩略图之Thumbnailator
- java程序测试之字节流
- HTML <;form>; 标签的 method 属性(20161028)
- OSS.Core基于Dapper封装(表达式解析+Emit)仓储层的构思及实现
- TDD 之 Dojo coding
- Python内置函数(35)——next
- (java基础)Java输入输出流及文件相关
- freemarker使用
- 串的模式匹配算法 ------ KMP算法
- 第六篇:Jmeter Ftp服务器的连接
- 函数中返回char *类型
- Reverse string using recursion