Java 写一个方法,计算有多少单词以字母“quot”开头;A「;
这是全部编程代码 我只想用java来解决这个问题。Java 写一个方法,计算有多少单词以字母“quot”开头;A「;,java,Java,这是全部编程代码 我只想用java来解决这个问题。publicstaticintcounta(String[]s){ class Main { public static void main(String[] args) { String[] fruits = new String [5]; fruits[0] ="APPLE"; fruits[1]="FIG"; fruits[2]="GRAPE"; fruits[3]="PEAR"; fr
publicstaticintcounta(String[]s){
class Main {
public static void main(String[] args) {
String[] fruits = new String [5];
fruits[0] ="APPLE";
fruits[1]="FIG";
fruits[2]="GRAPE";
fruits[3]="PEAR";
fruits[4]="APRICOT";
System.out.println(countA(fruits)+" word(s) begins with an \"A\"");
}
//Write a method that counts how many words begins with the letter "A"
/** @param s is the current array
* @return the number of words in s that begin with "A"
*/
public static int countA(String[] s){
}
}
int结果=0;
对于(int i=0;i
您可以对数组进行流式处理,并在筛选后对元素进行计数
public static int countA(String[] s){
int x = 0;
for(String y : s){
if(y.startsWith("A")) x++;
}
return x;
}
到目前为止你尝试了什么?那么,是什么阻止了你?欢迎访问stackoverflow.com。请花些时间阅读,特别是命名和。也请和。最后请阅读。我需要一种解决这个问题的方法。好的,你可以看看
String#chatAt
方法。迭代字符串数组,如果第一个字符(在索引0处)等于“A”,则递增计数器,并在末尾返回计数器。请不要鼓励只使用代码的答案。另外,请不要回答OP没有真正尝试过任何东西或没有付出努力的问题。拜托,我的错。下一次我将从“到目前为止你都做了些什么?”开始。现在我知道为什么了。请不要鼓励只使用代码的答案。另外,请不要回答OP没有真正尝试过任何东西或没有付出努力的问题。请
public static int countA(String[] s){
int x = 0;
for(String y : s){
if(y.startsWith("A")) x++;
}
return x;
}
Arrays.stream(fruits).filter(s -> s.startsWith("A")).count();