第一个问题在这里。已经做了一些研究,但没有运气。我认为我的代码中几乎所有内容都正确,但我无法让它工作。它需要从用户也输入的字符串或短语中读取单个字符,然后打印出找到它的次数。我是java初学者,非常感谢任何帮助!谢谢。
import java.util.Scanner;
public class CountCharacters{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int timesFound;
String stringSearched, characterSearched;
System.out.printf("Enter a character for which to search: ");
characterSearched = input.next();
System.out.printf("Enter the string to search: \n");
stringSearched = input.nextLine();
int numberOfCharacters = stringSearched.length();
timesFound = 0;
for (int x = 0; x < numberOfCharacters; x++)
{
char charSearched = characterSearched.charAt(0);
if ( charSearched == stringSearched.charAt(x))
timesFound++;
System.out.printf("\nThere are %d occurrences of \'%s\' in \"%s\"",
timesFound, characterSearched, stringSearched);
}
}
}