兩道普通的面試題

1.用System.out.print輸出如下圖形:

☆☆☆☆☆☆☆
☆☆☆☆☆☆
☆☆☆☆☆
☆☆☆☆
☆☆☆
☆☆


答案:

public class Start_test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        char c = 9734;
        int i = 7;
        while (i >= 1) {
            for (int j = 0; j < i; j++) {
                System.out.print(c);
            }
            i--;
            System.out.print("\n");
        }
    }

}


2 .統計指定字符串中含有各個字符的個數。

 "gfjsjgperjtpojewrjopeqjwrio34rengflkajsaljfrajfasnflanfla"

如下例子格式輸出:

g:3;f:6;j:9;


答案:

public class String_Count {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String str = "gfjsjgperjtpojewrjopeqjwrio34rengflkajsaljfrajfasnflanfla";
        int count = 0;
        String temp = "";
        while (!"".equals(str)) {
            temp = str.replace(str.substring(0, 1), "");
            count = str.length() - temp.length();
            System.out.print(str.substring(0, 1) +":"+ count + ";");
            str = temp;
            count = 0;
        }
    }
}

結果:g:3;f:6;j:9;s:3;p:3;e:4;r:5;t:1;o:3;w:2;q:1;i:1;3:1;4:1;n:3;l:4;k:1;a:6;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章