簡體   English   中英

如何打印數組?

[英]How do I print out an array?

除了似乎無法弄清楚如何顯示數組之外,我的練習幾乎完成了。 做法是從用戶那里獲取一組長度為10的字符串,並檢查字符串中是否存在與長度為10的數組相對應的數字0-9。

到目前為止,這就是我所擁有的。

import java.util.*;

public class CharacterFrequency {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int telNumber[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    System.out.print("Give me a word of 10 characters. "
            + "\nI will tell you if it contains any numbers, and how many: ");
    String word = keyboard.nextLine();
    int length = word.length();
    if (length < 10 || length > 10) {
        System.out.println("Wrong length, you have entered an invalid word.");
        System.exit(0);
    } else {


        for (int index = 0; index < word.length() - 1; index++) {
            char ch = word.charAt(index);
            if ('0' == ch) {
                telNumber[0]++;
            } else if ('1' == ch) {
                telNumber[1]++;
            } else if ('2' == ch){
                telNumber[2]++;
            } else if ('3' == ch){
                telNumber[3]++;
        }   else if ('4' == ch){
                telNumber[4]++;
        }else if ('5' == ch){
                telNumber[5]++;
        }else if ('6' == ch){
                telNumber[6]++;
        } else if ('7' == ch){
                telNumber[7]++;
        } else if ('8' == ch){
                telNumber[8]++;
        }else if ('9' == ch){
                telNumber[9]++;
        } else {
        System.out.println("\nWrong length, you have entered an invalid word.");
        System.exit(0);
    }
        }
        System.out.println();

    }
}
}

我不知道如何打印輸出如下所示:

計數0是(字符串中的數字0)

計數1是(字符串中數字1)

Count 2是(字符串中2的數量)

計數3是(字符串中3的數目)

Count 4是(字符串中4的數目)等。

通常,最簡單的打印int數組的方法可能是Arrays.toString(int[])

System.out.println(Arrays.toString(telNumber));

您的telNumber聲明應更像是,

int telNumber[] = new int[10]; // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

您的聲明會給每個元素一個等於其索引的初始計數(我很確定您希望它們為零)。 然后,按照您的要求獲取格式(使用printf()for循環)可能看起來像

for (int i = 0; i < telNumber.length; i++) {
  System.out.printf("Count of %d is %d%n", i, telNumber[i]);
}

最后,可以使用Character.digit(char, int)大大縮短您的long if elseCharacter.digit(char, int)例如

if (ch >= '0' && ch <= '9') {
    telNumber[Character.digit(ch, 10)]++;
} else {
    System.out.println("\nWrong length, you have entered an invalid word.");
    System.exit(0);
}

幾件事要改變:

1)telNumber數組初始化,另一個選擇是:

int telNumber[] = new int[10];

2)使用char避免基於char值的ascii值分配所有10個if。

telNumber[ch - '0'] ++;

更多信息: ascii表

3)遍歷數組並打印

for (int i = 0; i < telNumber.length; i++) {
                  System.out.printf("Count of %d is %d%n", i, telNumber[i]);
                }

4)將循環固定為:

for (int index = 0; index < word.length() ; index++)

因為在您的代碼中它不計算輸入中的最后一個字符。

固定代碼:

Scanner keyboard = new Scanner(System.in);
        int telNumber[] = { 0,0,0,0,0,0,0,0,0,0};

        System.out
                .print("Give me a word of 10 characters. "
                        + "\nI will tell you if it contains any numbers, and how many: ");
        String word = keyboard.nextLine();
        int length = word.length();
        if (length < 10 || length > 10) {
            System.out
                    .println("Wrong length, you have entered an invalid word.");
            System.exit(0);
        } else {

            for (int index = 0; index < word.length() ; index++) {
                char ch = word.charAt(index);
                if (ch >= '0' && ch <= '9') {
                telNumber[ch - '0'] ++;
                }
                else {
                  System.out.println("\nWrong length, you have entered an invalid word.");
                  System.exit(0);
                }
            }
            System.out.println();

        }
        for (int i = 0; i < telNumber.length; i++) {
              System.out.printf("Count of %d is %d%n", i, telNumber[i]);
            }
    }
public static void printIntArray(int[] array) {
    System.out.print("{");

    for (int i = 0; i < array.length - 1; i++)
        System.out.print (array[i] + ", ");

    System.out.println(array[array.length - 1] + "}");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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