java - 计算字符串上的匹配字符
问题描述
我被要求创建一个程序,要求用户输入两个输入,这两个输入都必须存储为字符串。第一个输入可以是一个或多个单词,第二个输入必须是一个单独的字符。在用户输入两个输入后,程序应该计算唯一的宪章出现在第一个字符串中的次数(如果有的话)。一旦完成第一个字符串的迭代,程序就应该输出第二个字符串的实例数。例子:
“测试中有 1 次出现‘e’。”
程序必须使用 while 循环和字符串值。这是我现在遵循教授建立的参数的解决方案
public static void main(String[] args) {
String inputEntry; // User's word(s)
String inputCharacter; // User's sole character
String charCapture; // Used to create subtrings of char
int i = 0; // Counter for while loop
int charCount = 0; // Counter for veryfiying how many times char is in string
int charCountDisplay = 0; // Displays instances of char in string
Scanner scan = new Scanner(System.in);
System.out.print("Enter some words here: "); // Captures word(s)
inputEntry = scan.nextLine();
System.out.print("Enter a character here: "); // Captures char
inputCharacter = scan.nextLine();
if (inputCharacter.length() > 1 || inputCharacter.length() < 1) // if user is not in compliance
{
System.out.print("Please enter one character. Try again.");
return;
}
else if (inputCharacter.length() == 1) // if user is in compliance
{
while( i < inputEntry.length()) // iterates through word(s)
{
charCapture = inputEntry.substring(charCount); // Creates substring of each letter in order to compare to char entry
if (charCapture.equals(inputCharacter))
{
++charCountDisplay;
}
++charCount;
++i;
}
System.out.print("There is " + charCountDisplay +
" occurrence(s) of " + inputCharacter + " in the test.");
}
}
这个迭代有一个错误。它不会计算 inputCharacter 变量的所有实例,而不管字符串上出现了多少实例,它只计数到一个。我知道问题出在这部分代码中:
while( i < inputEntry.length()) // iterates through word(s)
{
charCapture = inputEntry.substring(charCount); // Creates substring of each letter in order to compare to char entry
if (charCapture.equals(inputCharacter))
{
++charCountDisplay;
}
++charCount;
++i;
}
我只是无法安静地确定我做错了什么。在我看来,charCountDisplay 变量在每次迭代后都会恢复为零。不应该在一开始就声明变量来避免这种情况吗?...我是一个困惑的人。
解决方案
这是错误的
charCapture = inputEntry.substring(charCount);
不返回一个字符
尝试使用inputEntry.charAt(charCount)
另一个提示是将变量定义在靠近使用它们的位置而不是在方法的顶部,例如:
String inputEntry;
inputEntry = scan.nextLine();
更好的是内联
String inputEntry = scan.nextLine();
它将使您的代码更加简洁和可读。
执行代码的更简洁的方法是:
Scanner scan = new Scanner(System.in);
System.out.print("Enter some words here: "); // Captures word(s)
String inputEntry = scan.nextLine();
System.out.print("Enter a character here: "); // Captures char
String inputCharacter = scan.nextLine();
// validate
// then
int len = inputEntry.length();
inputEntry = inputEntry.replace(inputCharacter, "");
int newlen = inputEntry.length();
System.out.format("There is %d occurrence(s) of %s in the test.%n",
len - newlen, inputCharacter);
输出
Enter some words here: scarywombat writes code
Enter a character here: o
There is 2 occurrence(s) of o in the test.
推荐阅读
- sql - 由于组子句,用于比较具有不同值的相同字段的 Sql 查询
- c# - 自定义 ASP.NET 的 DataList 控件的 HTML 输出
- angular - How to hidden the message when loading in html page?
- spring-rabbit - 与 Java 1.6 兼容的 spring-rabbit 示例
- hibernate - 从多对多关系中检索的 HQL 语法
- javascript - not able to target required data with jquery
- php - Yii2 Swift_SmtpTransport gmail 不工作
- mysql - 使用 MySQL 中的某些 JSON 列加载查询得到 {Invalid JSON text: "Invalid value." 在列 } 值的位置 0
- jquery - 在执行单击操作之前,jquery 脚本不起作用
- angular - 标头未发送 angular-5