我试图在Java中编写一个for循环,该循环将计算字符串中字母的出现次数。 用户将输入要计数的字母和要搜索的字符串。 这是一个非常基本的代码,我们还没有涉及数组或其他很多东西。 (我意识到我曾两次宣告过信件,但此时我的大脑已经死了)这是我到目前为止已经尝试过并且遇到的麻烦,感谢您的帮助:
好的,我根据建议更改了代码,但现在只读了句子的第一个单词?
import java.util.Scanner;
public class CountCharacters {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char letter;
String sentence ="";
System.out.println("Enter a character for which to search");
letter = in.next().charAt(0);
System.out.println("Enter the string to search");
sentence = in.next();
int count = 0;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (ch == letter) {
count++;
}
}
System.out.printf("There are %d occurrences of %s in %s", count,
letter, sentence);
}
}
由于您的代码无法编译,您怎么知道它不起作用? 它没有,但是您实际尝试过吗? 如果有,请说明问题所在(除了重复声明之外)。
我看到几个问题。首先,您有两个具有相同名称的变量。
其次,您的if条件检查语句的长度是否大于0,而不是检查字符是否相等。
Scanner in = new Scanner(System.in);
char inLetter ="";
String sentence ="";
System.out.println("Enter a character for which to search");
inLetter = in.next().charAt(0);
System.out.println("Enter the string to search");
sentence = in.next();
int letter = 0;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (inLetter == ch) {
letter++;
}
}
System.out.print(sentence.charAt(letter));
我也强烈建议验证输入(在上面的示例中未完成),而不是仅仅假设您从第一个输入中得到一个字符,在第二个输入中得到一句话。
读取字符后,您的Scanner类尚未移至下一行
letter = in.next().charAt(0);
在读取输入字符串之前添加另一个in.nextLine()
System.out.println("Enter a character for which to search");
letter = in.next().charAt(0);
in.nextLine();
System.out.println("Enter the string to search");
sentence = in.nextLine();
旧线程,但希望这会有所帮助:)
尝试indexOf()方法。
它应该工作
希望这对您有所帮助。
import java.util.Scanner;
public class CountCharacters {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the string to search");
String sentence = in.nextLine();
System.out.println("Enter a character for which to search");
String letter = in.next();
int noOfOccurance = 0;
for (int i = 0; i < sentence.length(); i++) {
char dh=letter.charAt(0);
char ch = sentence.charAt(i);
if (dh==ch) {
noOfOccurance++;
}
}
System.out.print(noOfOccurance);
}
}
样本输入输出:
Enter the string to search
how are you
Enter a character for which to search
o
No of Occurances : 2
您需要知道要搜索的字符。您可以使用char charToSearch = letter.toCharArray()[0];
定义一个变量,例如count以计算给定字符串中字母的出现次数。
循环字符串并比较每个字符,如果字符等于要搜索的字符,则计数++;
例子->
int count = 0;
char charToSearch = letter.toCharArray()[0];
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == charToSearch) {
count++;
}
}
System.out.printf("Occurrences of a %s in %s is %d", letter, sentence, count);
无需循环:
String sentence ="abcabcabcd";
String letter ="b";
int numOfOccurences = sentence.length() -
sentence.replaceAll(letter,"").length();
System.out.println("numOfOccurences ="+numOfOccurences);
输出:
numOfOccurences = 3
if (sentence.length() <= 0) {
letter++;
}
程序中的上述代码部分是错误的。除非您输入一个空字符串,否则永远不会如此。
基本上这不是正确的逻辑。您将必须使用直接比较。
尝试这个
忘记String letter =""
忘记letter = in.next()
// There's no nextChar() method, so this is a work aroung
char ch = in.findWithinHorizon(".", 0).charAt(0);
int letter = 0;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ch) {
letter++;
}
}
System.out.println(letter); // print number of times letter appears
// You don't want this
System.out.print(sentence.charAt(letter)); // Makes no sense
尝试这个:
Char letter = '';
String sentence ="";
System.out.println("Enter a character for which to search");
letter = in.next().charAt(0);
System.out.println("Enter the string to search");
sentence = in.next();
int count= 0;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (ch==letter) {
count++;
}
}
System.out.print(letter+" occurance:"+count);
您不能申请in.next(); 与字符。
行动! 修复..
您的if (sentence.length() <= 0) {不正确。更改您的条件,例如:
System.out.println("Enter a character for which to search");
letter = in.next();
System.out.println("Enter the string to search");
sentence = in.next();
char searchLet=letter.charAt(0); // Convert String to char
int letter = 0;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (searchLet== ch) { // Check the occurrence of desired letter.
letter++;
}
}
System.out.print(sentence.charAt(letter));