Java 如何等于字符串中的单个字符,然后计算它
“这是我的代码”Java 如何等于字符串中的单个字符,然后计算它,java,string,equals,Java,String,Equals,“这是我的代码” publicstaticvoidmain(字符串[]args){ 整数字母计数=0; String check_word=新字符串(“如何等于字符串中的单个字符,然后计算它”); 字符串单_字母=”; int i=0; 对于(i=0;i代码>方法>代码> i ,而不是 0 。这将返回简单的char,因此您需要将其与=进行比较,比如检查单词.charAt(i)='a' 另一种(可能更好)方法是使用 for (char ch : check_word.toCharArray()){
publicstaticvoidmain(字符串[]args){
整数字母计数=0;
String check_word=新字符串(“如何等于字符串中的单个字符,然后计算它”);
字符串单_字母=”;
int i=0;
对于(i=0;i您的问题之一是存在代码>在你的if(单字母等于(“a”)
条件之后,所以你的代码
if (single_letter.equals("a") ); {
letter_count ++;
}
实际上与
if (single_letter.equals("a") ){
//empty block "executed" conditionally
}
//block executed regardless of result in `if` condition
{
letter_count ++;
}
另一个问题是
single_letter = check_word.substring(0);
将从索引0
中获取check\u word
的子字符串,这意味着它将存储与check\u word
相同的字符串。考虑使用<代码> CARAT >代码>方法>代码> i <代码>,而不是<代码> 0 。这将返回简单的char
,因此您需要将其与=
进行比较,比如检查单词.charAt(i)='a'
另一种(可能更好)方法是使用
for (char ch : check_word.toCharArray()){
//test value of ch
}
您的问题之一是存在代码>在你的if(单字母等于(“a”)
条件之后,所以你的代码
if (single_letter.equals("a") ); {
letter_count ++;
}
实际上与
if (single_letter.equals("a") ){
//empty block "executed" conditionally
}
//block executed regardless of result in `if` condition
{
letter_count ++;
}
另一个问题是
single_letter = check_word.substring(0);
将从索引0
中获取check\u word
的子字符串,这意味着它将存储与check\u word
相同的字符串。考虑使用<代码> CARAT >代码>方法>代码> i <代码>,而不是<代码> 0 。这将返回简单的char
,因此您需要将其与=
进行比较,比如检查单词.charAt(i)='a'
另一种(可能更好)方法是使用
for (char ch : check_word.toCharArray()){
//test value of ch
}
您似乎对子字符串函数的作用感到困惑。这一行:
single_letter = check_word.substring(0);
本质上返回整个检查单词
,并将其存储在单个字母
中。我怀疑你真正想要的是:
single_letter = check_word.substring(i, i + 1);
在那个位置得到一个字母
您还可以将其更改为:
if(check_word.charAt(i) == 'a') {
letter_count++;
}
您似乎对子字符串函数的作用感到困惑。这一行:
single_letter = check_word.substring(0);
本质上返回整个检查单词
,并将其存储在单个字母
中。我怀疑你真正想要的是:
single_letter = check_word.substring(i, i + 1);
在那个位置得到一个字母
您还可以将其更改为:
if(check_word.charAt(i) == 'a') {
letter_count++;
}
试试看
publicstaticvoidmain(字符串[]args){
整数字母计数=0;
char[]check_word=“如何等于字符串中的单个字符,然后计算它”
.toCharArray();
字符单字母='a';
for(int i=0;i
试试
public static void main(String[] args) {
int letter_count = 0;
char[] check_word = "How to equals a single character in string and then calculate it "
.toCharArray();
char single_letter = 'a';
for (int i = 0; i < check_word.length; i++) {
if (single_letter == check_word[i]) {
letter_count++;
}
}
System.out.println(" - \"a\"" + " was found " + letter_count + " times");
}
publicstaticvoidmain(字符串[]args){
整数字母计数=0;
char[]check_word=“如何等于字符串中的单个字符,然后计算它”
.toCharArray();
字符单字母='a';
for(int i=0;i
为什么不使用如下字符:
public static void main(String[] args) {
int letter_count = 0;
String check_word = new String ("How to equals a single character in string and then calculate it ");
char toCheck = 'a';
for (int i = 0; i < check_word.length(); i++) {
char cursor = check_word.charAt(i);
if (cursor == toCheck) {
letter_count++;
}
}
System.out.println ( " - \"a\"" + " was found " + letter_count + " times");
}
publicstaticvoidmain(字符串[]args){
整数字母计数=0;
String check_word=新字符串(“如何等于字符串中的单个字符,然后计算它”);
char-toCheck='a';
对于(int i=0;i
为什么不使用如下字符:
public static void main(String[] args) {
int letter_count = 0;
String check_word = new String ("How to equals a single character in string and then calculate it ");
char toCheck = 'a';
for (int i = 0; i < check_word.length(); i++) {
char cursor = check_word.charAt(i);
if (cursor == toCheck) {
letter_count++;
}
}
System.out.println ( " - \"a\"" + " was found " + letter_count + " times");
}
publicstaticvoidmain(字符串[]args){
整数字母计数=0;
String check_word=新字符串(“如何等于字符串中的单个字符,然后计算它”);
char-toCheck='a';
对于(int i=0;i
“谢谢你的代码”除了上面的注释外,我还要指出一个明显的问题——如果
,请删除后面的semoclon。我知道,但是当我运行字符串时(如何在字符串中等于一个字符,然后再计算它?-“a”被发现了60次。我可以猜出你所说的“等于一个字符”是什么意思——我猜你的意思是什么“看看这个字符是否等于另一个字符。”我不知道“计算”是什么意思。@yshavit他们想计算一个特定字符在字符串中出现的次数。在这种情况下,字符串中的字符a
如何等于字符串中的一个字符,然后计算它”感谢代码“除了上面的注释之外,我还要指出一个明显的注释——删除if
后面的semoclon。我知道,但是当我运行字符串时(如何等于字符串中的一个字符,然后计算它?-“a”被发现了60次。我可以猜出你所说的“等于一个字符”是什么意思——我猜你是什么意思。”“看看这个字符是否等于另一个字符。”我不知道“计算它”“表示。@yshavit他们想计算某个字符在字符串中出现的次数。在这种情况下,字符串中的字符a
如何等于字符串中的单个字符,然后计算它。
。还有这个校验字。子字符串(0);应该是这个校验字。子字符串。”