【发布时间】:2013-11-08 09:15:37
【问题描述】:
我必须编写一个程序来计算字符串中字母 B 的数量。我已经得到了那部分,但它还要求我使用一个静态方法,该方法根据字符串中是否有任何 B 来返回 true 或 false,我真的不知道如何放入该部分。
import java.util.Scanner;
public class CountB {
// write the static method “isThisB” here
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a string: ");
String w = keyboard.nextLine();
int count=0;
for (int i=0; i<w.length(); i++)
{
if (w.charAt(i)=='B'||w.charAt(i)=='b')
{
count++;
}
}
System.out.println("Number of B and b: "+ count);
}
}
【问题讨论】:
-
好了,先创建静态方法。你认为方法签名应该是什么?
-
选择你要转成方法的表达式,在键盘上按CTRL+ALT+M提取方法(在IntelliJ上,其他IDE有不同的热键)
-
男人。 StackOverflow 很烂。人们只是写作业,而且是免费的。