gpt4 book ai didi

Java : Occurances of a character in a String

转载 作者:行者123 更新时间:2023-12-01 13:18:48 27 4
gpt4 key购买 nike

我需要有关 Java 代码的帮助。

这就是问题所在:

输入示例:AaaaaAa

输出:A 出现 7。

问题是我需要它来忽略案例。

请帮助我,我的代码工作正常,只是它不忽略大小写。

import java.io.*;

public class letter_bmp{
public static BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
public static void main(String[] args) throws Exception

{

String string1;
String pick;

String ans;
do
{
int count=0;
System.out.print("En taro Adun, Executor! Input desired string : ");
string1 = input.readLine();
System.out.print("Now, Executor...which character shall I choose : ");
pick = input.readLine();

for(int counter = 0; counter < string1.length(); counter++)
{
if(pick.charAt(0) == string1.charAt(counter))
count++;
}
System.out.print("Executor...you picked '" + pick + "' it is used " + count + " times in the word "+string1+".");

System.out.println("\nWould you like to try again, Executor? (Yes/No): ");
ans = input.readLine();
}
while(ans.equalsIgnoreCase("Yes"));
}

}

最佳答案

使用 String.toLowerCase() 将字符串转换为小写字符方法。

// ...
string1 = input.readLine().toLowerCase();
// ...
pick = input.readLine().toLowerCase();
// ...

关于Java : Occurances of a character in a String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22224229/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com