【发布时间】:2018-11-21 10:41:49
【问题描述】:
String sentence = JOptionPane.showInputDialog (null, "Write a sentence.");
String letter = JOptionPane.showInputDialog(null, "Write a letter");
while (true) {
if (letter.equals("Stop"))
System.exit(0);
//to calculate number of specific character
else {
int countLetter = 0;
int L = letter.length();
for (int i = 0; i < L; i++) {
if ((letter.charAt(i) = .....))
countLetter++;
}
}
}
是否可以替换点以使程序计算给定字母在第一个字符串中写入的句子中出现的次数?
【问题讨论】:
-
当然,但它是
==,而不是=,您可能还想循环浏览sentence。换句话说,除了.....之外,您还缺少一段代码 -
欢迎来到 Stack Overflow,Sandra。再看一遍你的代码,我认为你的想法有一些错误。您正在迭代
letter中的字符,但letter应该只包含一个字母,对吗?所以迭代它对我来说似乎是一个错误。我认为您应该在代码中将其替换为sentence,然后检查sentence.charAt(i) == letter.charAt(0)
标签: java