问题描述
我试图在Java中编写一个for循环来计算字符串中字母的出现次数。用户将输入要计数的字母和要搜索的字符串。这是一个非常基本的代码,我们还没有得到数组或其他许多东西。 (我意识到我宣布了两封信,但是我的大脑在这一点上已经死了)这是我迄今为止所尝试过的,遇到了麻烦,任何帮助表示赞赏:好吧我改变了我的代码每个建议,但现在它只是读取我的句子的第一个字?
import java。 util.Scanner;
public class CountCharacters {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
char char;
String sentence =;
System.out.println(输入要搜索的字符);
letter = in.next()。charAt(0);
System.out.println(输入要搜索的字符串);
sentence = in.next();
int count = 0;
for(int i = 0; i< sentence.length(); i ++){
char ch = sentence.charAt(i);
if(ch == letter){
count ++;
$ b $ System.out.printf(%s%s中有%d次发生,count,
letter,sentence);
$ b $ / code $ / $ p
解决方案我看到一些问题。首先你有两个同名的变量。
其次,如果条件检查句子的长度大于0而不是检查字符是否相等。
扫描仪输入= new Scanner(System.in);
char inLetter =;
String sentence =;
System.out.println(输入要搜索的字符);
inLetter = in.next()。charAt(0);
System.out.println(输入要搜索的字符串);
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个句子。
I am trying to write a for loop in Java that will count the occurrences of a letter in a string. The user will enter the letter to count and the string in which to search. This is a very basic code, and we have not gotten to arrays or much else yet. (I realize that I declared letter twice, but my brain is dead at this point) This is what I have tried so far and am having trouble with, any help is appreciated:
Ok I changed my code per suggestions, but now it is only reading the first word of my sentence?
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); } }解决方案I see a couple of issues. First you have two variables with the same name.
Second your if condition check for the lenght of the sentence to be greater then 0 instead of checking for character equality.
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));I would also strongly suggest to validate the input (which is not done in the example above) instead of just assuming you got 1 character from the first input and 1 sentence in the second.
这篇关于计算字符串中字母的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!