1. 借助随机函数生成100以内的整数表示成绩,将得到的10名同学3门课的成绩存储在一个二维数组中。分别统计每个人的平均成绩,每门课的平均成绩,并分别显示。

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
47
48
49
50
import java.util.Random;

public class Test {
public static void main(String[] args) {
int[][] score =new int[10][3];
Random r = new Random();
for (int i = 0; i < score.length; i++) {
for (int j = 0; j < score[i].length; j++) {
score[i][j] = r.nextInt(100);
}
}

// 显示成绩
for (int i = 0; i < score.length; i++) {
for (int j = 0; j < score[i].length; j++) {
System.out.print(score[i][j] + " ");
}
System.out.println("");
}

System.out.println("----------------------------");
// 求每个人的平均成绩
int[] personal_avg_score = new int[10];
for (int i = 0; i < score.length; i++) {
int sum = 0;
for (int j = 0; j < score[i].length; j++) {
sum += score[i][j];
}
personal_avg_score[i] = sum / score[i].length;
}

for (int i = 0; i < personal_avg_score.length; i++) {
System.out.println("个人的平均成绩:" + personal_avg_score[i]);
}

// 求每科的平均成绩
int[] subject_avg_score = new int[3];
for (int i = 0; i < score[i].length; i++) {
int sum = 0;
for (int j = 0; j < score.length; j++) {
sum += score[j][i];
}
subject_avg_score[i] = sum / score.length;
}

for (int i = 0; i < subject_avg_score.length; i++) {
System.out.println("科目:" + subject_avg_score[i]);
}
}
}

2. 编写程序统计用户从键盘输入的任意一个字符串中数字字符出现的总数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;
public class CountCharacter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String str = sc.next();

int count_num = 0;
int count_letter = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isDigit(ch)) {
count_num++;
} else if(Character.isLetter(ch)) {
count_letter++;
}
}

System.out.println("数字的个数:" + count_num);
System.out.println("字符的个数:" + count_letter);
System.out.println("其他的个数:" + (str.length() - (count_num + count_letter)));
}
}

3. 编程实现:获取某个给定的整型数组中最小的能整除5的数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Arrays;
import java.util.Random;

public class findMinNum {
public static void main(String[] args) {
Random r = new Random();
int[] arr = new int[50];
for (int i = 0; i < arr.length; i++) {
int temp = r.nextInt(100) + 1;
if (temp % 5 == 0) {
arr[i] = temp;
} else {
arr[i] = 999;
}
}
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
if (arr[0] == 999) {
System.out.println("数组中没有能被5整除的数字");
} else {
System.out.println("数组中最小能被5整除的数字是:" + arr[0]);
}
}
}

4. 编程实现:判断用户从键盘输入的任意字符串,在忽略了非数字和字母符号后,

是否为回文串。例如:用户输入的字符串是“123ab-&*;2332@b#a3%21”,忽略非数

字字母符号后的串为“123ab2332ba321”,这是一个回文串(正向和逆向的序列相

同)。

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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Palindromic_String {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String str = sc.next();
sc.close();

List<String> checkInput = new ArrayList();
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i)) || Character.isDigit(str.charAt(i))) {
checkInput.add(Character.toString(str.charAt(i)));
}
}

List<String> checkReverse= new ArrayList();
for (int i = 0; i < checkInput.size(); i++) {
checkReverse.add(checkInput.get(checkInput.size() - i - 1));
}

if (checkReverse.equals(checkInput)) {
System.out.println("是回文串");
} else {
System.out.println("不是回文串");
}
}
}

5. 从给定的二进制文件test.dat中读取50个整数,过滤3和5的倍数,将剩余元素送到

屏幕上显示。

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
import java.io.*;


public class FileDataFilter {
public static void readDigs(File fd, int[] digs) throws IOException {
DataInputStream din = new DataInputStream(new BufferedInputStream(new FileInputStream(fd)));
for (int i = 0; i < digs.length; i++) {
digs[i] = din.readInt();
din.close();
}
}

public static void printDigs(int[] digs) {
for (int i = 0; i < digs.length; i++) {
if (digs[i] % 3 == 0) {
continue;
}
if (digs[i] % 5 == 0) {
continue;
}
System.out.print(digs[i]);
}
System.out.println();
}


public static void main(String[] args) throws IOException{
File fd = new File("test.dat");
int[] digs = new int[0];
FileDataFilter.readDigs(fd,digs);
FileDataFilter.printDigs(digs);
}
}

6. 编写一个Java程序,求出整数1~100内的所有素数,并在屏幕上显示这些数,每5

个一行。同时算出这些素数的累加和,并独立一行显示到屏幕上。

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
public class PrimeNumber {
public static void main(String[] args) {
int sum = 0;
int changeLine = 0;
for (int i = 2; i < 100; i++) {
if (isPrime(i)) {
sum += i;
System.out.print(i + " ");
changeLine++;
if (changeLine == 5) {
System.out.println("");
changeLine=0;
}
}
}
System.out.println(sum);
}
public static boolean isPrime(int x) {
for (int i = 2; i < x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
}

7. 编程实现:随机生成100个不大于100的正整数,去除重复值后,按一行5个将不重

复的数输出到屏幕上,并显示不重复数一共有多少个。

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
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.function.Consumer;

public class RandomNumber {
public static void main(String[] args) {
Random r = new Random();
Set s = new HashSet();
for (int i = 0; i < 101; i++) {
s.add(r.nextInt(100) + 1);
}

s.forEach(new Consumer() {
int index = 0;
@Override
public void accept(Object o) {
index++;
System.out.print(o + " ");
if (index == 5) {
System.out.println();
index = 0;
}
}
});

System.out.println();
System.out.println("不重复数的个数为:" + s.size());
}
}

8. 列出当前文件夹及其子文件夹下所有文件的文件名(文件夹的名字无需列出)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.File;

public class ShowFileName {
public static void listAll(File fd) {
File[] FList = fd.listFiles();
for (int i = 0; i < FList.length; i++) {
if (FList[i].isFile()) {
System.out.println(FList[i].getName());
} else {
listAll(FList[i]);
}
}
}

public static void main(String[] args) {
File fd = new File(".");
listAll(fd);
}
}

9. 编写一个矩形类,包含:

长和宽两个属性;

依据指定的长和宽构建矩形;

不指定长和宽时,默认两者为1.0;

能求矩形的面积;

能求矩形的周长;

能显示矩形的长和宽的基本信息,

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
class RectangleClass {
private double width;
private double height;


// 默认值
public RectangleClass() {
this.width = this.height = 1.0;
}

// 初始化值
public RectangleClass(int width, int height) {
this.width = width;
this.height = height;
}

// 获取面积
public double getArea() {
return this.height * this.width;
}

// 获取周长
public double getLength() {
return (this.height * 2) + (this.width * 2);
}

// 获取、设置长宽
public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}
}

10. 编程实现:从键盘输入若干个正整数,输入为负数时表示输入结束。求输入的若

干元素中的最大值,以及它们的累加和、乘积,并将结果送到屏幕显示。

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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

class ScannerTest {
public static void main(String[] args) {
// 输入数据且判断退出时机
Scanner sc = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
while (true) {
System.out.println("请输入整数:");
String s = sc.next();
if (Integer.parseInt(s) >= 0) {
list.add(Integer.parseInt(s));
} else if (Integer.parseInt(s) < 0) {
break;
}
}

// 将输入数据转换为数组
int[] arr = new int[list.size()];
int sum = 0;
int cj = 1;
for (int i = 0; i < list.size(); i++) {
arr[i] = list.get(i);
// 计算累加和以及乘积
sum += list.get(i);
cj *= list.get(i);
}

// 排序得到最大值
Arrays.sort(arr);
System.out.println("最大值:");
System.out.println(arr[arr.length - 1]);
System.out.println("累加和:");
System.out.println(sum);

System.out.println("乘积:");
System.out.println(cj);
}
}

11.\ 随机生成50个不大于1000的正整数,将大于等于平均值的那些正整数按个位数的

升序排列,并将它们以5个一行,空格间隔的形式输出到屏幕。

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.*;

class RandomSort {
public static void main(String[] args) {
Random r = new Random();
int[] arr = new int[50];

// 生成随机数
int avgNum = 0;
int sum = 0;
for (int i = 0; i < arr.length; i++) {
arr[i] = r.nextInt(1001);
sum += arr[i];
}

// 计算平均值
avgNum = sum / arr.length;
System.out.println(avgNum);

// 过滤出高于平均值的数
int arrLength = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= avgNum) {
arrLength++;
}
}

// 将高于平均值的数重新组成一个数组
int[] avgArr = new int[arrLength];
int index = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= avgNum) {
avgArr[index] = arr[i];
index++;
}
}

System.out.println("平均值:" + avgNum);
System.out.println(Arrays.toString(avgArr));

// 冒泡排序,对个位数进行升序排序
for (int i = 0; i < avgArr.length - 1; i++) {
for (int j = 0; j < avgArr.length - 1; j++) {
if (avgArr[j] % 10 > avgArr[j + 1] % 10) {
int tmp = avgArr[j];
avgArr[j] = avgArr[j + 1];
avgArr[j + 1] = tmp;
}
}
}

// 输出格式化
int changeLine = 0;
for (int i = 0; i < avgArr.length; i++) {
changeLine++;
System.out.print(avgArr[i] + " ");
if (changeLine == 5) {
System.out.println();
changeLine = 0;
}
}
}
}

12. 编程实现:定义通信簿中的一个联系方式,内容包括:

身份证,姓名和电话三个属性;

能够依据给定的身份证、姓名和电话生成一个联系项(构造函数);

能修改联系电话;

能依据身份证号查找某个联系项是否与当前项相同(equals方法);

能够显示详细的联系项信息。

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
import java.util.Objects;

class AddressList {
private String id;
private String name;
private int phone;

// 构造函数生成练习簿
public AddressList(String id, String name, int phone) {
this.id = id;
this.name = name;
this.phone = phone;
}

// 修改号码
public void setPhone(int phone) {
this.phone = phone;
}

// 比较身份证的值(这地方存疑,答案上是这么写的)
public boolean equals(AddressList a) {
return a.id.equals(this.id);
}

// 输出详细信息
@Override
public String toString() {
return "AddressList{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", phone=" + phone +
'}';
}
}

测试类:

1
2
3
4
5
6
7
8
9
10
public class AddressListTest {
public static void main(String[] args) {
AddressList adL1 = new AddressList("1234567890x","zhangsan",1333333333);
AddressList adL2 = new AddressList("1234567890","zhangsan",1333333333);

System.out.println(adL1.equals(adL2));
adL1.setPhone(1344444444);
System.out.println(adL1.toString());
}
}