This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Q1

Request: receive a string s from the user’s input from the command line,

  1. Counts and prints the number of uppercase letters, lowercase letters, digits, and other characters.
  2. Take a character from the command line and look for the number of occurrences and the position of the character in the string s.
  3. Within the class, in addition to main, define at least two static methods that implement the above functions.
public static void main(String[] args) {
		while (true) {
			try {
				System.out.println("Q1: Please enter 1\nQ2: please enter 2\n Exit: please enter 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("Please enter a string :");
					String input = sc2.nextLine();
					Count(input);
					System.out.println();
				}
				if (num == 2) {
					@SuppressWarnings("resource")
					Scanner sc2 = new Scanner(System.in);
					System.out.println("Please enter a string :");
					String input2 = sc2.nextLine();
					@SuppressWarnings("resource")
					Scanner sc3 = new Scanner(System.in);
					System.out.println(Please enter a search string:);
					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("Enter 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("Number of uppercase letters :\t" + e);
		System.out.println("Number of lowercase letters :\t" + e2);
		System.out.println("Number of digits :\t" + n);
		System.out.println("Other characters :\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 repeat position :\t" + i + "\n");
		}
		System.out.println("\n" + ss + "\t repeats :\t" + n);
	}
Copy the code

Q2

  1. Defines an array describing the set of vertices of a two-dimensional polygon, traversing the array to output the coordinate values of all the points
  2. Define the student class, and access the instance members and class members of the class, instance methods, and class methods
public static void main(String[] args) {
		while (true) {
			try {
				System.out.println("Q1: Please enter 1\nQ2: please enter 2\n Exit: please enter 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("Zhang".181203601.89);
					stu[1] = new Student("Bill".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("Enter 1 Or 2 Or 3! \n"); }}}public static void Point(a) {
		int[][] a = { { 1.1}, {-1.1 }, { 1, -1}, {-1, -1}}; System.out.println("Initialize the set of regular quadrilateral vertices as :");
		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(a) {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getId(a) {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public int getScore(a) {
			return score;
		}

		public void setScore(int score) {
			this.score = score;
		}

		public Student(String name, int id, int score) {
			super(a);this.name = name;
			this.id = id;
			this.score = score;
		}

		public void show(a) {
			System.out.println("Student Id:" + id + "\ T Name:" + name + "\t grade:" + 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;
					}
				}
			}
		}
	}
Copy the code