我必须使用递归编写一个程序,它会提示输入一个单词,然后计算字母a出现在该单词中的次数。
我需要输入什么而不是+ count +才能使其正常工作?
这是我到目前为止所拥有的;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter String");
String line = input.nextLine();
char p = 'a';
}
public static int count (String line, char p){
int len = line.length();
if ((len == 0) || (p == '\0')){ // case for empty string or nil char.
return 0; // recursion ends here
}
String rest = line.substring(1);
if (line.charAt(0) == p) {
return count(rest, p) + 1; // recurse on substring
} else {
} // end of if-else
return count(rest, p); // recurse on substring
}
System.out.println("Character a occurs " + count + " times ");
}
答案 0 :(得分:0)
删除代码底部的内容:
{
System.out.println("Character a occurs " + count + " times ");
}
在主要方法中添加以下行:
System.out.println("Character a occurs " + count(line,p) + " times ");
答案 1 :(得分:0)
您应该调用方法计数并在main方法中显示结果。
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter String");
String line = input.nextLine();
char p = 'a';
System.out.println("Character a occurs " + count(line, p) + " times ");
}
答案 2 :(得分:0)
你总是需要递归,这只是你是否增加。你应该在代码中表达它。
您只需使用一条(可读)行替换整个方法:
public static int count(String line, char p) {
return line.isEmpty() ? 0 : count(line.substring(1), p) + (line.charAt(0) == p ? 1 : 0);
}
将结果打印到屏幕应该来自您的主要方法。
答案 3 :(得分:0)
好像你还没有调用你的计数方法..
试试这个..
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter String");
String line = input.nextLine();
char p = 'a';
int count = count(line, p);
System.out.println("Character a occurs " + count + " times ");
}
public static int count (String line, char p)
{
int len = line.length();
if ((len == 0) || (p == '\0')) // case for empty string or nil char.
return 0; // recursion ends here
String rest = line.substring(1);
if (line.charAt(0) == p)
{
return count(rest, p) + 1; // recurse on substring
}
else {
} // end of if-else
{
return count(rest, p); // recurse on substring
}
}
答案 4 :(得分:0)
另一种方式可以是
import java.util.Scanner;
public class test{
static int counter=0;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter String");
String line = input.nextLine();
char p = 'a';
count(line, p);
System.out.println("Character a occurs " + counter + " times ");
}
public static void count (String line, char p){
int len = line.length();
if ((len == 0) || (p == '\0')){ // case for empty string or nil char.
return; // recursion ends here
}
String rest = line.substring(1);
if (line.charAt(0) == p) {
counter++; // recurse on substring
} else {
} // end of if-else
count(rest, p); // recurse on substring
}
}
答案 5 :(得分:0)
您的源代码中存在很多错误。您应该在短期内尝试这一点,并长期阅读Deitel& Deitel的Java How To Program一书,以提高您的编程技能。
import java.util.Scanner;
public class test{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter String");
System.out.println("Character a occurs " + ((Integer)count(input.nextLine(), 'a')).toString() + " times.");
input.close();
}
public static int count (String line, char p){
if (line.length() == 0) { // base case: no more chars to look at
return 0;
}
if (line.charAt(0) == p) {
return count(line.substring(1), p) + 1; // recurse on substring
}
else {
return count(line.substring(1), p); // recurse on substring
}
}
}