Example 71
topic
If the annual growth rate of my country's GNP is 10%, what percentage will my country's GNP increase in n years compared with the present?
analyze
Calculation formula:
$$p = (1+r)^n$$
where r is the annual growth rate, n is the number of years, and p is the multiple compared to now.
accomplish
import java.util.Scanner;
import static java.lang.Math.pow;
/**
* Created with IntelliJ IDEA.
*
* @Package : PACKAGE_NAME
* @ClassName : Example71.java
* @CreateTime : 2022/4/1 23:11
* @Version : 1.0
* @Author : 村雨
* @Email : 747731461@qq.com
* @公众号 : 村雨遥
* @Website : https://cunyu1943.github.io
* @Description :
*/
public class Example71 {
public static final double R = 0.1;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年数");
int n = scanner.nextInt();
System.out.format("%d 年后相比现在增长了:%.2f 倍\n", n, pow(1 + R, n));
}
}
result
Example 72
topic
Suppose you have 1,000 yuan and want to save it for 5 years, you can follow the following 5 deposit methods:
- 5 years at a time
- First deposit for 2 years, then deposit principal and interest for another 3 years after maturity
- Deposit for 3 years first, then deposit the principal and interest for another 2 years after maturity
- First deposit for 1 year, then deposit the principal and interest for another 1 year after maturity, for 5 consecutive years
- Survival period, current interest is settled quarterly
The interest on a bank deposit in a certain year is as follows:
- 1-year term: 4.14%
- 2-year term: 4.68%
- 3-year term: 5.4%
- 5-year term: 5.85%
- Current: 0.72%
analyze
Assuming that r is the annual interest rate and n is the number of years of deposit, the formula for calculating the sum of principal and interest is:
1 year term: $p=1000*(1+r)$
n years: $p==1000*(1+n*r)$
Deposit n times 1 year: $p = 1000*(1+r)^n$
Demand deposit: $p=1000*(1+r/4)^{4n}$
accomplish
import java.util.Scanner;
import static java.lang.Math.pow;
/**
* Created with IntelliJ IDEA.
*
* @Package : PACKAGE_NAME
* @ClassName : Example71.java
* @CreateTime : 2022/4/1 23:12
* @Version : 1.0
* @Author : 村雨
* @Email : 747731461@qq.com
* @公众号 : 村雨遥
* @Website : https://cunyu1943.github.io
* @Description :
*/
public class Example72 {
public static final double R0 = 0.0072;
public static final double R1 = 0.0414;
public static final double R2 = 0.0468;
public static final double R3 = 0.054;
public static final double R5 = 0.0585;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入本金");
float p = scanner.nextFloat();
System.out.format("一次存 5 年:%.2f\n", p * (1 + R5 * 5));
System.out.format("先存 2 年,到期后本息存 3 年:%.2f\n", p * (1 + 2 * R2) * (1 + 3 * R3));
System.out.format("先存 3 年,到期后本息存 2 年:%.2f\n", p * (1 + 2 * R2) * (1 + 3 * R3));
System.out.format("存 1 年期,到期后本息再存,连续存 5 年:%.2f\n", p * pow(1 + R1, 5));
System.out.format("存活期:%.2f\n", p * pow(1 + R0 / 4, 4 * 5));
}
}
result
Example 73
topic
Set the radius of the circle r, the height of the cylinder h, find the circumference of the circle, the area of the circle, the surface area of the sphere, the volume of the sphere, and the volume of the cylinder.
analyze
The corresponding evaluation formula:
- Circumference: $2πr$
- Circle area: $πr^2$
- Sphere surface area: $4πr^2$
- Sphere volume: $4πr^3/3$
- Cylinder volume: $πr^2h$
accomplish
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @Package : PACKAGE_NAME
* @ClassName : Example73.java
* @CreateTime : 2022/4/1 23:12
* @Version : 1.0
* @Author : 村雨
* @Email : 747731461@qq.com
* @公众号 : 村雨遥
* @Website : https://cunyu1943.github.io
* @Description :
*/
public class Example73 {
public static final double PI = 3.141526;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入圆半径 r,圆柱高 h");
float r = scanner.nextFloat();
float h = scanner.nextFloat();
System.out.format("圆周长:%.2f\n", 2 * PI * r);
System.out.format("圆面积:%.2f\n", PI * r * r);
System.out.format("圆球表面积:%.2f\n", 4 * PI * r * r);
System.out.format("圆球体积:%.2f\n", 4 / 3 * PI * r * r * r);
System.out.format("圆柱体积:%.2f\n", h * PI * r * r);
}
}
result
Example 74
topic
There is a function:
$$y=\begin{cases}x,x<1\\2x-1,1<=x<10\\3x-11,x>=10\end{cases}$$
Enter the value of $x$ and output the corresponding value of $y$.
analyze
There is nothing to say, directly use the conditional control, through the judgment of the $x$ value, and then calculate the corresponding $y$ value and output.
accomplish
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @Package : PACKAGE_NAME
* @ClassName : Example74.java
* @CreateTime : 2022/4/1 23:12
* @Version : 1.0
* @Author : 村雨
* @Email : 747731461@qq.com
* @公众号 : 村雨遥
* @Website : https://cunyu1943.github.io
* @Description :
*/
public class Example74 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入 x");
int x = scanner.nextInt();
if (x < 1) {
System.out.format("x = %d,y = %d\n", x, x);
} else if (x < 10) {
System.out.format("x = %d,y = %d\n", x, 2 * x - 1);
} else {
System.out.format("x = %d,y = %d\n", x, 3 * x - 11);
}
}
}
result
Example 75
topic
A 100-point scale is given, and the grades A, B, C, D, and E are required to be output. Above 90 is A, 80 to 89 is B, 70 to 79 is C, 60 to 69 is D, and below 60 is E.
analyze
Either by judging the interval to which the score belongs, or by first dividing the score by 10, then judging the obtained quotient, and outputting the corresponding grade of the score.
accomplish
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @Package : PACKAGE_NAME
* @ClassName : Example75.java
* @CreateTime : 2022/4/1 23:13
* @Version : 1.0
* @Author : 村雨
* @Email : 747731461@qq.com
* @公众号 : 村雨遥
* @Website : https://cunyu1943.github.io
* @Description :
*/
public class Example75 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char grade = 'A';
System.out.println("请输入学生成绩");
int score = scanner.nextInt();
while (score < 0 || score > 100) {
System.out.println("输入有误,请重新输入");
score = scanner.nextInt();
}
switch (score / 10) {
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
grade = 'E';
break;
}
System.out.format("成绩为:%d,相应等级为:%c", score, grade);
}
}
result
finally
Regarding the code of this article, I have uploaded it to the open source platform, and readers who need it can pick it up by themselves.
java-programming-instance
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。