2022年09月30日Java上机

示例5 -1

JBT5101

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
public class JBT5101 {
public static void main(String args[]) {
/*数组定义方式1*/
char CHA[];
CHA = new char[4];
CHA[0] = 'A';
CHA[1] = 'B';
CHA[2] = 'C';
CHA[3] = 'D';
show(CHA, "CHA"); //输出数组内容
/*数组定义方式2*/
char CHB[] = {'1', '2', '3', '4', '5'};
show(CHB, "CHB"); //输出数组内容
/*数组定义方式3*/
char CHC[] = new char[3];
CHC[0] = 'a';
CHC[1] = 'b';
CHC[2] = 'c';
show(CHC, "CHC"); //输出数组内容
}

static void show(char chs[], String str) {
for (int i = 0; i < chs.length; i++) {
System.out.println(str + "[" + i + "]=" + chs[i]);
}
}
}

示例5-4

JBT5104

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class JBT5104 {
public static void main(String[] args) {
int i, j, Num = 9;
int b[][] = new int[Num][];
for (j = 1; j <= Num; j++) {
b[j - 1] = new int[j];
for (i = 1; i <= b[j - 1].length; i++)
b[j - 1][i - 1] = i * j;
}
for (j = 1; j <= b.length; j++) {
for (i = 1; i <= b[j-1].length;
i++)
System.out.print(i + "*" + j + "=" + b[j-1][i-1]+" ");
System.out.println("");
}
}
}

示例5-5

JBT5201

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class JBT5201 {
public static void main(String args[]) {
int i;
/*字符串定义方式1*/
String str1;
str1 = new String();
str1 = "This is A method of creating string..";
System.out.println("str1=" + str1);
/*字符串定义方式2*/
String str2;
str2 = new String("This is B method of creating string..");
System.out.println("str2=" + str2);
/*字符串定义方式3*/
String str3 = new String("This is C method of creating string…");
System.out.println("str3=" + str3);
/*字符串组定义方式4*/
String str4;
str4 = "This is D method of creating string…";
System.out.println("str4=" + str4);
/*字符串定义方式5*/
String str5 = "This is E method of creating string…";
System.out.println("str5=" + str5);
}
}

示例5-7

JBT5203

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class JBT5203 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a, b);
System.out.println("a=" + a + " b=" + b);
}

static void operate(StringBuffer x, StringBuffer y) {
x.append(y);
y = x;
System.out.println("x=" + x + " y=" + x);
}
}

示例5-8

JBT5301

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class JBT5301 {
public static void main(String args[]) {
MyArray my1 = new MyArray();
char ch = '*';
char chset[] = {'a', 'b', 'c', '*'};
String strarr = new String(chset);
System.out.println(strarr + "中字符" + ch + "的个数为" + my1.countChar(strarr, ch));
}
}

class MyArray {
//统计str中字符ch的个数
public int countChar(String str, char ch) {
int i, j = 0;
for (i = 0; i < str.length(); i++)
if (str.charAt(i) == ch) j = j + 1;
return j;
}
}

扩展题

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
public class ArrayTest {
private static final int MAX = 10;
public static void main(String[] args) {
int[] array = new int[MAX];
System.out.print("原始数组:");
for (int i = 0; i < MAX; i++) {
array[i] = (int) ((Math.random() * 100) + 1);//生成100以内的随机数
System.out.print(array[i]);
if (i != MAX - 1) {
System.out.print(",");
}
}
System.out.println();
int max = array[0]; // 把数据中的第1个元素存max
int min = array[0]; // 把数据中的第1个元素存min
int sum = array[0];
for (int i = 1; i < array.length; i++) { // 从第二个元素开始遍历数组
if (array[i] > max) { // 假如元素大于max 就把当前值赋值给max
max = array[i];
}
if (array[i] < min) { // 假如元素小于min 就把当前值赋值给min
min = array[i];
}
sum += array[i];
}
System.out.println("数组中的最大值:" + max);
System.out.println("数组中的最小值:" + min);
System.out.println("数组元素的和:" + sum);
System.out.println("数组元素的平均值:" + sum / array.length);
}
}

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