我正在尝试打印字符串中三个字母单词的数量。下面的代码输出字符串中0-3个字母单词的数量。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Input a string:");
String s;
s = input.nextLine();
String[] strings = s.split(" ");
int[] counts = new int[4];
for(String str : strings)
if(str.length() < counts.length) counts[str.length()] += 1;
for(int i = 1; i < counts.length; i++)
System.out.println(i + " letter words: " + counts[i]);
input.close();
// TODO Auto-generated method stub
}//end main
}//end class
如何修改此代码只显示三个字母的数字?有什么想法吗?
答案 0 :(得分:0)
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Input a string:");
String s;
s = input.nextLine();
String[] strings = s.split(" ");
int[] counts = new int[4];
for(String str : strings)
if(str.length() < counts.length) counts[str.length()] += 1;
System.out.println(3 + " letter words: " + counts[3]);
input.close();
// TODO Auto-generated method stub
}//end main
}
它可能看起来像那样,ommited loop for(int i = 1; i < counts.length; i++)
。