2022年09月16日Java上机

课上练习

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Square {
public static int getArea(int a) {
return a;
}
public static int getGirth(int a) {
return a*4;
}
public static void main(String[] args) {
int a = 4;
System.out.println(getArea(a));
System.out.println(getGirth(a));
}
}

示例1-3

JBT13

1
2
3
4
5
6
7
8
9
public class JBT13 {
public static void main(String args[]) {
JBT14 obj1=new JBT14();
JBT15 obj2=new JBT15();
int x=1,y=2;
obj1.add(x,y);
obj2.minus(x,y);
}
}

JBT14

1
2
3
4
5
class JBT14 {
void add(int x, int y) {
System.out.println(x + "+" + y + "=" + (x + y));
}
}

JBT15

1
2
3
4
5
public class JBT15 {
void minus(int x, int y) {
System.out.println(x + "-" + y + "=" + (x - y));
}
}

示例2-2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class JBT2202 {
public static void main(String args[]) {
//输出byte类型的最大值和最小值
System.out.println("byte型最大值= " + Byte.MAX_VALUE);
System.out.println("byte型最小值= " + Byte.MIN_VALUE);
//输出short 类型的最大值和最小值
System.out.println("short型最大值= " + Short.MAX_VALUE);
System.out.println("short型最小值= " + Short.MIN_VALUE);
//输出int 类型的最大值和最小值
System.out.println("int型最大值= " + Integer.MAX_VALUE);
System.out.println("int型最小值= " + Integer.MIN_VALUE);
//输出long类型的最大值和最小值
System.out.println("long型最大值= " + Long.MAX_VALUE);
System.out.println("long型最小值= " + Long.MIN_VALUE);
//输出float类型的最大值和最小值
System.out.println("float型最大值= " + Float.MAX_VALUE);
System.out.println("float型最小值= " + Float.MIN_VALUE);
//输出double类型的最大值和最小值
System.out.println("double型最大值= " + Double.MAX_VALUE);
System.out.println("double型最小值= " + Double.MIN_VALUE);
}
}

示例2-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class JBT2203 {
public static void main(String args[]) {
byte a = 120;
short b = 255;
int c = 2200;
long d = 2147483649L;
float f1, f2;
double g = 0.1234567812345678;
b = a; //低精度赋值给高精度
c = (int) d; //高精度强制转换赋值给低精度
f1 = a; //低精度赋值给高精度
f2 = (float)g; //高精度强制转换赋值给低精度
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
System.out.println("d=" + d);
System.out.println("f1=" + f1);
System.out.println("f2=" + f2);
System.out.println("g=" + g);
}
}

示例2-6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Scanner;
public class JBT2402 {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.print("输入岗位等级: ");
int grade = stdin.nextInt();
System.out.println("");
//String grade stdin.nextLine();
int wage = 0;
if (grade == 1) {
wage = 280;
} else if (grade == 2) {
wage = 1900;
} else if (grade == 3) {
wage = 1630;
} else if (grade == 4) {
wage = 1420;
} else if (grade == 5) {
wage = 1180;
} else if (grade == 6) {
wage = 1040;
} else if (grade == 7) {
wage = 930;
} else if (grade == 8) {
wage = 780;
} else if (grade == 9) {
wage = 730;
} else if (grade == 10) {
wage = 680;
} else if (grade == 11) {
wage = 620;
} else if (grade == 12) {
wage = 590;
}
System.out.printf("岗位等级%2d的岗位工资:%4d\n", grade, wage);
}
}

示例2-9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class JBT2404 {
public static void main(String[] args) {
int n = 10, m = 5;
System.out.println("for循环求解下的结果是=" + sum1(n, m));
System.out.println("while循环求解下的结果是=" + sum2(n, m));
System.out.println("do while循环求解下的结果是=" + sum3(n, m));
}

//for
public static long sum1(int n, int m) {
int i = 0;
long sum = 0;
for (i = 1; i <= n; i++) {
if (i % m == 0) {
sum = sum + i;
}
}
return sum;
}

//while
public static long sum2(int n, int m) {
int i = 0;
long sum = 0;
while (i <= n) {
if (i % m == 0) {
sum = sum + i;
}
i = i + 1;
}
return sum;
}

//do while
public static long sum3(int n, int m) {
int i = 0;
long sum = 0;
do {
if (i % m == 0) {
sum = sum + i;
}
i = i + 1;
} while (i <= n);
return sum;
}
}

2022年09月16日Java上机
https://www.eldpepar.com/coding/45936/
作者
EldPepar
发布于
2022年9月16日
许可协议