1.求出上面数组中0-9分别出现的次数
@Test
public void zuoye01(){
/* 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分别出现的次数
双重for循环*/
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};
for(int num = 0; num <= 9; num++){ //数字是从0-9
int count01 = 0;
for(int i = 0; i <= scores.length - 1; i++){
if(num == scores[i]){ //运行300次
count01++;
}
}
System.out.println(num+": "+count01);
}
}
@Test
public void zuoye01(){
/* 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分别出现的次数
双重for循环*/
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};
for(int num = 0; num <= 9; num++){ //数字是从0-9
int count01 = 0;
for(int i = 0; i <= scores.length - 1; i++){
if(num == scores[i]){ //运行300次
count01++;
}
}
System.out.println(num+": "+count01);
}
}
2.改进版,用十个计数器,这样统计一遍就行
@Test
public void zuoye0101(){
/* 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分别出现的次数
双重for循环*/
int[] array={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[] countarray = new int[10]; //弄十个计数器,这样统计一遍就可以把个数统计完全
for (int i = 0;i < array.length;i++) {
countarray[array[i]]++; //走一遍就可以
}
for (int i = 0; i < 10;i++) {
System.out.println(i+"的个数为:"+countarray[i]);
}
}
@Test
public void zuoye0101(){
/* 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分别出现的次数
双重for循环*/
int[] array={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[] countarray = new int[10]; //弄十个计数器,这样统计一遍就可以把个数统计完全
for (int i = 0;i < array.length;i++) {
countarray[array[i]]++; //走一遍就可以
}
for (int i = 0; i < 10;i++) {
System.out.println(i+"的个数为:"+countarray[i]);
}
}
3.输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数。
@Test
public void zuoye18(){
System.out.println("请输入字符串:");
Scanner scanner = new Scanner(System.in);
String string01 = scanner.nextLine(); //遇到回车就结束,可以接收空格
// String string01 = scanner.next(); //这个.next() 遇到空格或者是回车就结束了
System.out.println(string01);
char[] array = string01.toCharArray();//变成字符数组
int lettercount = 0;
int spacecount = 0;
int numbercount = 0;
int othercount = 0;
for (int i = 0; i < array.length ; i++) {
if((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z')){
lettercount++;
}else if(array[i] == ' ' ){
spacecount++;
}else if(array[i] >= '0' && array[i] <= '9'){
numbercount++;
}else{
othercount++;
}
}
System.out.println("英文字母:"+lettercount);
System.out.println("空格:"+spacecount);
System.out.println("数字:"+numbercount);
System.out.println("其他:"+othercount);
}
@Test
public void zuoye18(){
System.out.println("请输入字符串:");
Scanner scanner = new Scanner(System.in);
String string01 = scanner.nextLine(); //遇到回车就结束,可以接收空格
// String string01 = scanner.next(); //这个.next() 遇到空格或者是回车就结束了
System.out.println(string01);
char[] array = string01.toCharArray();//变成字符数组
int lettercount = 0;
int spacecount = 0;
int numbercount = 0;
int othercount = 0;
for (int i = 0; i < array.length ; i++) {
if((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z')){
lettercount++;
}else if(array[i] == ' ' ){
spacecount++;
}else if(array[i] >= '0' && array[i] <= '9'){
numbercount++;
}else{
othercount++;
}
}
System.out.println("英文字母:"+lettercount);
System.out.println("空格:"+spacecount);
System.out.println("数字:"+numbercount);
System.out.println("其他:"+othercount);
}