[英]Basic array issue in Java
寻找有关基本阵列问题的帮助。 程序必须读取一个句子并将单词长度的频率存储在一个数组中,然后打印出1个字母单词,2个字母单词等几个单词。
我是一个非常原始的Java程序员,但在下面进行了刺刺,将不胜感激一些指导。 我似乎编译了什么,但是在运行程序并输入句子时吐出了一些乱码。
当我在程序中输入一个句子时,会得到如下输出 :
[I@eb42cbf
[I@eb42cbf
[I@eb42cbf
[I@eb42cbf
[I@eb42cbf
[I@eb42cbf
我的代码:
class WordCount
{
public static void main(String[] args)
{
int wordList[] = new int[20];
System.out.println("Please enter a sentence.");
for (int i = 0; i <= wordList.length; i++)
{
String s = Console.readToken();
int x = s.length();
wordList[x]++;
}
int x = 1;
while (x < wordList.length)
{
if (wordList[x] > 0)
System.out.println(x + "-letter words: " + wordList[x]);
x++;
}
}
}
这是我的建议:
这是我的版本-更改为:使用扫描仪,将x重命名为i并将while循环更改为for循环:
public static void main(String[] args) {
int wordList[] = new int[20];
System.out.println("Please enter a sentence.");
Scanner scanner = new Scanner(System.in); //Use a scanner, part of the standard JDK
for (int i = 0; i <= wordList.length; i++) {
String s = scanner.next(); //reads the next string
int length = s.length();
wordList[length]++;
}
for (int i = 0; i < wordList.length; i++) { //use a for loop
if (wordList[i] > 0) {
System.out.println(i + "-letter words: " + wordList[i]);
}
}
}
如果不应该while循环
while(i < wordlist.Length){
if (wordlist[i] > 0){
System.out.println(i + "-letter words: " + wordlist[i]);
}
i++;
}
private static long wordcount(String line){
long numWords = 0;
int index = 0;
boolean prevWhiteSpace = true;
while(index < line.length()){
char c = line.charAt(index++);
boolean currWhiteSpace = Character.isWhitespace(c);
if(prevWhiteSpace && !currWhiteSpace){
numWords++;
}
prevWhiteSpace = currWhiteSpace;
}
return numWords;
}
如果您的意思是当输入一个句子,例如:“这是一个句子”时,输出应为:
4个字母的单词:2
2个字母的单词:1
1个字母的单词:1
8个字母的单词:1
我的代码:
import java.util.HashMap;
import java.util.Scanner;
class WordCount {
public static void main(String[] args) {
HashMap<Integer, Integer> statistic = new HashMap<Integer, Integer>();
System.out.println("Please enter a sentence.");
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String[] words = s.split(" ");
for (int i = 0; i < words.length; i++) {
if (statistic.containsKey(words[i].length())) {
Integer value = statistic.get(words[i].length());
statistic.put(words[i].length(), value + 1);
} else
statistic.put(words[i].length(), 1);
}
for (Integer num : statistic.keySet()) {
Integer key = num;
Integer value = statistic.get(num);
System.out.println(key + "-letter words: " + value);
}
}
}
另一种方法:
int wordList[] = new int[20];
System.out.println("Please enter a sentence.");
String s = "";
try{
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
s = bufferRead.readLine();
}
catch(IOException e){
e.printStackTrace();
}
String[] s1 = s.split(" ");
for(int i = 0 ; i < s1.length ; i++){
int len = s1[i].length();
wordList[len]++;
}
for (int i = 0; i < wordList.length; i++) { //use a for loop
if (wordList[i] > 0) {
System.out.println(i + "-letter words: " + wordList[i]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.