簡體   English   中英

Java中的基本數組問題

[英]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++;
        }
    }
}

這是我的建議:

  • 使用Scanner而不是Console.readToken,它不是標准JDK的一部分
  • 使用標准變量名稱(計數器的i優於x)
  • 您的其余代碼工作正常
  • 但我會使用for循環,而不是使用while循環,因為您知道需要循環多少次

這是我的版本-更改為:使用掃描儀,將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM