1、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
求出上面数组中0-9分别出现的次数
public void test1() {
int[] count = new int[10];
int[] scores = new int[]{0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
for(int i = 0; i < scores.length; i++) {
count[scores[i]]++;
}
for(int i = 0; i < 10; i++) {
System.out.println(i + "出现的次数是:" + count[i]);
}
}
2、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
要求求出其中的奇数个数和偶数个数。
public void test2() {
int[] scores = {0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
int sum1 = 0, sum2 = 0;
for(int i = 0; i < scores.length; i++) {
if(scores[i] % 2 != 0) {
sum1++;
} else {
sum2++;
}
}
System.out.println("奇数个数:" + sum1 + " 偶数个数:" + sum2);
}
3、题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
char[] array = {'a' , 'b', 'c', 'b' , 'a'};
public void test3() {
char[] array = new char[]{'a','b','c','b','a'};
for(int i = 0; i < array.length / 2; i++) {
if(array[i] != array[array.length - 1 - i]) {
System.out.println("不是回文");
return;
}
}
System.out.println("是回文");
}
4、输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数。
public void test4() {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
char[] ch = s.toCharArray();
int alpha = 0, blank = 0, num = 0, other = 0;
for(int i = 0; i < ch.length; i++) {
if(ch[i] <= '9' && ch[i] >= '0') {
num++;
} else if((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')) {
alpha++;
} else if(ch[i] == ' ') {
blank++;
} else {
other++;
}
}
System.out.println("英文字母的个数:" + alpha + " 空格的个数:" + blank + " 数字的个数:" + num + " 其他字符的个数:" + other);
}