forked from Doragd/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
- 写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母) 1 <=n <= 1000
- 分别获取两次的值去循环比较统计
- 获取两次的值注意不区分大小问题
- int index,count = 0 这种定义编译错误,Java中局部变量初始化要等用到才会
- 记得输出打印结果
- 切记byte占用一个字节 -2的7次方-1 ----2的7次方 大概 -128 -127
- short 占用两个字节 2的15 肯定大于2的10次方 注意细节处理
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//获取输入的第一个字符串 转换为数组
char[] array = in.nextLine().toLowerCase().toCharArray();
//获取第二次输入的字符
char searchChar = in.nextLine().toLowerCase().charAt(0);
// 遍历数组 统计相等的个数
// int i, count = 0; Java中这种写法只有count被初始化 然而i并没有初始化,使用才会被初始化,导致编译错误
int index = 0;
int count = 0;
while (index < array.length) {
if (array[index] == searchChar) {
count += 1;
}
index++;
}
System.out.println(count);
}
}