简单程序编写

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

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]);
}

[quote][color=red]2.输入输出[/color][/quote]

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();
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值