我正在尝试在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);
}
}
答案 0 :(得分:1)
我看到了几个问题。首先,您有两个具有相同名称的变量。
第二个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));
我还强烈建议验证输入(在上面的示例中没有完成),而不是假设你从第一个输入中得到1个字符,在第二个输入中得到1个句子。
答案 1 :(得分:0)
您的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));
答案 2 :(得分:0)
试试这个:
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);
答案 3 :(得分:0)
试试这个
忘记String letter = ""
&lt; - 删除
忘记letter = in.next()
&lt; - 删除
// 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
答案 4 :(得分:0)
if (sentence.length() <= 0) {
letter++;
}
程序中的上述代码部分是错误的。除非你输入一个空字符串,否则这将永远不会成立。
基本上这不是正确的逻辑。您将不得不使用直接比较。
答案 5 :(得分:0)
无需循环:
String sentence = "abcabcabcd";
String letter = "b";
int numOfOccurences = sentence.length() -
sentence.replaceAll(letter, "").length();
System.out.println("numOfOccurences = "+numOfOccurences);
<强>输出:强>
numOfOccurences = 3
答案 6 :(得分:0)
实施例---&GT;
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);
答案 7 :(得分:0)
希望这对你有所帮助。
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
答案 8 :(得分:0)
尝试indexOf()方法。 它应该工作
答案 9 :(得分:0)
您的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();
旧线程,但希望这会有所帮助:)