注意:把class后面的类名改成你新建的文件名,然后把下面的代码拷贝运行即可
01: 200个鸡蛋,每打12,几打?余几个?
public class case01 {
public static void main(String[] args) {
System.out.println("200个鸡蛋是" + 200/12 + "打");
System.out.println("剩余" + 200%12 + "个");
}
}
运行结果:
02:150/0.02/10
public class case02 {
private static double money(double x, double d, int z) {
for (int i = 1; i <= z; i++) {
x = x*(1.0+d);
}
return x;
}
public static void main(String[] args) {
double cash = 1500000.0;
double lilv = 0.02;
int year = 10;
double result = money(cash, lilv, year);
System.out.println("年利率" + lilv + ",
" + cash + "元,
" + "存" + year + "年,
最后" + result + "元钱。");
}
}
运行结果:
03: 圆面积
public class case03 {
// 定义PI常量
final static double PI = 3.1415;
// 面积计算方法
public static double area(float r) {
double result = PI*r*r;
return result;
}
public static void main(String[] args) {
System.out.printf("半径为10的圆面积是:%.2f
", area(10));
System.out.printf("半径为20的圆面积是:%.2f
", area(20));
System.out.printf("半径为50的圆面积是:%.2f
", area(50));
}
}
运行结果:
04:奇数
import java.util.Scanner;
public class case04 {
private static void show() {
Scanner sc = new Scanner(System.in);
for (int i = 1; i < 150; i++) {
System.out.print("Please input a:");
int a = sc.nextInt();
System.out.print("Please input b:");
int b = sc.nextInt();
int mininum = Math.min(a, b);
int maxinum = Math.max(a, b);
for (int j = mininum; j <= maxinum; j++) {
if (j%2 == 0) {
continue;
}
System.out.print(j + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
show();
}
}
运行结果
09: for/while/do-while 写99乘法表
public class case09 {
// for 循环实现99乘法表
private static void showDo() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "x" + i + "=" + j*i + " ");
}
System.out.println();
}
}
// while 循环实现99乘法表
private static void showWhile() {
int i = 1;
while(i <= 9) {
int j = 1;
while(j <= i) {
System.out.print(j + "x" + i + "=" + j*i + " ");
j++;
}
System.out.println();
i++;
}
}
// do/while 循环实现99乘法表
private static void showDoWhile() {
int i = 1;
do {
int j = 1;
do {
System.out.print(j + "x" + i + "=" + j*i + " ");
j++;
} while(j <= i);
System.out.println();
i++;
}while(i <= 9);
}
public static void main(String[] args) {
System.out.println("
for循环写的:");
showDo();
System.out.println("
while循环写的:");
showWhile();
System.out.println("
do/while循环写的:");
showDoWhile();
}
}
运行结果: