我被要求创建一个程序,要求用户提供两个输入,这两个输入都必须存储为字符串。第一个输入可以是一个或多个单词,第二个输入必须是一个唯一字符。在用户输入两个输入之后,程序应计算多少次(如果有的话)唯一的宪章出现在第一个字符串中。一旦完成第一个字符串的迭代,程序便应输出第二个字符串的实例数。例如:
“测试中有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.
这里是完整的MVCE:
package com.example.countcharacters;]包>
/** * EXAMPLE OUTPUT: * Enter some words here: * How now brown cow * Enter a character here: * abc * Please enter one character. Try again. * Enter a character here: * o * There are 4 occurrence(s) of o in the text How now brown cow. */ import java.util.Scanner; public class CountCharacters { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // Captures word(s) String inputEntry; System.out.println("Enter some words here: "); inputEntry = scan.nextLine(); // Captures char char inputCharacter; while (true) { System.out.println("Enter a character here: "); String line = scan.nextLine(); if (line.length() == 1) { inputCharacter = line.charAt(0); break; } else { // if user is not in compliance System.out.println("Please enter one character. Try again."); } } // iterates through word(s) int charCountDisplay = 0; int i = 0; while(i < inputEntry.length()) { char c = inputEntry.charAt(i++); if (c == inputCharacter) { ++charCountDisplay; } } // Print results System.out.print("There are " + charCountDisplay + " occurrence(s) of " + inputCharacter + " in the text " + inputEntry + "."); } }
注意:
'希望有帮助!