Java——多维字符串 与 字符串处理

原程序下载(点击此处下载)

Q1

要求:从命令行接收用户的输入一个字符串s,

  1. 统计并输出大写字母、小写字母、数字和其它字符的数量。
  2. 接收命令行输入的一个字符,在字符串s中查找该字符出现的次数以及位置。
  3. 在类内,除了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

  1. 定义一个数组,描述一个二维多边形顶点集合,遍历数组输出所有点的坐标值
  2. 定义学生类,访问类的实例成员和类成员 、实例方法和类方法
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;
					}
				}
			}
		}
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GodOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值