简单程序编写

引用

1.一个字符串(其中包含汉字),写一段程序,统计这个字符串出现次数最多的字符,并打印此字符出现了几次

		public void getMax(String a){
			char array[] = a.toCharArray();
		char arrayb[] = a.toCharArray();
		int[] sum = new int[array.length];
		// 计算出字符串中每个字符出现的次数,用sum存储。。
		for (int i = 0; i < array.length; i++) {
			int count = 0;
			for (int j = 0; j < arrayb.length; j++) {
				if (array[i] == arrayb[j]) {
					count++;
				}
			}
			sum[i] = count;
		}
		// 取出次数最大的那个字符的下标。。。
		int max = sum[0];
		int index = 0;
		for (int m = 0; m < sum.length; m++) {
			if (max < sum[m]) {
				max = sum[m];
				index = m;
			}
		}
		System.out.println("出现次数最多的字符:" + array[index]);
		System.out.println("出现的次数:" + sum[index]);
	}

引用
2.输入输出

1  2  3  4  5  6  7  
24 25 26 27 28 29 8  
23 40 41 42 43 30 9  
22 39 48 49 44 31 10 
21 38 47 46 45 32 11 
20 37 36 35 34 33 12 
19 18 17 16 15 14 13
import java.util.Scanner;

public class SnakePrint {
	static int length;
	static {
		System.out.println("请输入圈的大小:");
		Scanner scanner = new Scanner(System.in);
		length= scanner.nextInt();
		
	};
	static int value = 1;
	static int[][] snake = new int[length][length];
	static Direction lastDirection = Direction.Right;

	static enum Direction {
		Right, Down, Left, Up;
	}

	static int space;
	public static void initialArray() {
		int row = 0, line = 0;
		for (int c = 0; c < length * length; c++) {
			snake[row][line] = value;
			lastDirection = findDirection(row, line);
			switch (lastDirection) {
			case Right:
				line++;
				break;
			case Down:
				row++;
				break;
			case Left:
				line--;
				break;
			case Up:
				row--;
				break;
			default:
				System.out.println("error");
			}
			if(c==length*length-1){
				space=String.valueOf(value).length();
			}
			value++;
		}
	}

	@SuppressWarnings("static-access")
	static Direction findDirection(int row, int line) {
		Direction direction = lastDirection;
		switch (direction) {
			case Right: {
				if ((line == length - 1) || (snake[row][line + 1] != 0))
					direction = direction.Down;
				break;
			}
			case Down: {
				if ((row == length - 1) || (snake[row + 1][line] != 0))
					direction = direction.Left;
				break;
			}
			case Left: {
				if ((line == 0) || (snake[row][line - 1] != 0))
					direction = direction.Up;
				break;
			}
			case Up: {
				if (snake[row - 1][line] != 0)
					direction = direction.Right;
				break;
			}
		}
		return direction;
	}

	public static void main(String[] args){
		initialArray();
		// display.....
		for (int i = 0; i < length; i++) {
			for (int j = 0; j < length; j++) {
					System.out.print(snake[i][j] + " ");
					int count=String.valueOf(snake[i][j]).length();
					for(int k=0;k<space-count;k++){//调整格式
						System.out.print(" ");
					}
			}
			System.out.println();
		}
	}
}


你可能感兴趣的:(C++,c,C#,J#,UP)