Q1
要求:从命令行接收用户的输入一个字符串s,
- 统计并输出大写字母、小写字母、数字和其它字符的数量。
- 接收命令行输入的一个字符,在字符串s中查找该字符出现的次数以及位置。
- 在类内,除了main以外,至少定义两个静态方法实现上述功能。
public static void main(String[] args) {
while (true) {
try {
System.out.println("Q1:请输入 1\nQ2:请输入 2\n退出:请输入3");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (num == 1) {
@SuppressWarnings("resource")
Scanner sc2 = new Scanner(System.in);
System.out.println("请输入字符串:");
String input = sc2.nextLine();
Count(input);
System.out.println();
}
if (num == 2) {
@SuppressWarnings("resource")
Scanner sc2 = new Scanner(System.in);
System.out.println("请输入字符串:");
String input2 = sc2.nextLine();
@SuppressWarnings("resource")
Scanner sc3 = new Scanner(System.in);
System.out.println("请输入搜索字符串:");
String again = sc3.nextLine();
Repeat(input2, again);
System.out.println();
}
if (num == 3) {
System.out.println("Bye!");
System.exit(0);
}
} catch (Exception e) {
System.out.println("请输入 1 Or 2 Or 3!\n");
}
}
}
public static void Count(String s) {
int e = 0;
int e2 = 0;
int n = 0;
int o = 0;
char[] a = s.toCharArray();
for (char b : a) {
if (b >= 65 && b <= 90) {
e++;
} else if (b >= 97 && b <= 122) {
e2++;
} else if (b >= 48 && b <= 57) {
n++;
} else {
o++;
}
}
System.out.println("大写字母个数:\t" + e);
System.out.println("小写字母个数:\t" + e2);
System.out.println("数字个数:\t" + n);
System.out.println("其他字符:\t" + o);
}
public static void Repeat(String s, String ss) {
int index = 0;
int n = 0;
int fromIndex = 0;
while ((index = s.indexOf(ss, fromIndex)) != -1) {
n++;
fromIndex = index + ss.length();
}
for (int i = -1; i <= s.lastIndexOf(ss); ++i) {
i = s.indexOf(ss, i);
System.out.print(ss + "\t的重复位置:\t" + i + "\n");
}
System.out.println("\n" + ss + "\t的重复次数:\t" + n);
}
Q2
- 定义一个数组,描述一个二维多边形顶点集合,遍历数组输出所有点的坐标值
- 定义学生类,访问类的实例成员和类成员 、实例方法和类方法
public static void main(String[] args) {
while (true) {
try {
System.out.println("Q1:请输入 1\nQ2:请输入 2\n退出:请输入3");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (num == 1) {
Point();
System.out.println();
}
if (num == 2) {
Student[] stu = new Student[3];
stu[0] = new Student("张三", 181203601, 89);
stu[1] = new Student("李四", 181203602, 65);
stu[2] = new Student("scott", 181203603, 77);
Student.sort(stu);
for (Student s : stu) {
s.show();
}
System.out.println();
}
if (num == 3) {
System.out.println("Bye!");
System.exit(0);
}
} catch (Exception e) {
System.out.println("请输入 1 Or 2 Or 3!\n");
}
}
}
public static void Point() {
int[][] a = { { 1, 1 }, { -1, 1 }, { 1, -1 }, { -1, -1 } };
System.out.println("初始化正四边形顶点集合为:");
for (int i = 0; i < a.length; i++) {
System.out.print("(");
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%2d", a[i][j]);
if (j == 0) {
System.out.print(",");
}
}
System.out.print(")" + "\n");
}
}
static class Student {
private String name;
private int id;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student(String name, int id, int score) {
super();
this.name = name;
this.id = id;
this.score = score;
}
public void show() {
System.out.println("学号:" + id + "\t姓名:" + name + "\t成绩:" + score);
}
public static void sort(Student[] stu) {
Student s;
for (int i = 0; i < stu.length - 1; i++) {
for (int j = 0; j < stu.length - i - 1; j++) {
if (stu[j].score > stu[j + 1].score) {
s = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = s;
}
}
}
}
}