2015-10-26 29 views
0

下面是程序中的指令/例子,我必須做的:編寫一個程序,確定多少每個字母的名字有

您好!請輸入您的名字。你好

謝謝。現在輸入你的姓。 Doody

你的名字叫Howdy,你的姓氏是Doody。什麼是好的 姓名你有!

你的名字包含5個字母,你的姓氏包括5 字母。

你的英文縮寫是:H d

您的名字包含:

3 D的

1小時

3 O的

1瓦特

2 Y的

好吧,你好,Doody,這很有趣。再見!

我可以一路獲得,直到名稱中的字母數量。我的老師說了一些關於ASCII字符集的東西,我只需要1個循環。我不知道大部分的數組和其他類型的東西,我想要一些幫助如何使它成爲一個循環。這是我到目前爲止的代碼:

import java.util.Scanner; 

public class CoolSet2Problem4 
{ 
    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Hello! Please enter your first name in all lower case:"); 
     String firstname = keyboard.next(); 
     System.out.println(); //this is just so it looks nice with another space 
     System.out.println("Thank you. Now enter your last name in all lower case:"); 
     String lastname = keyboard.next(); 
     System.out.println(); //this is just so it looks nice with another space 
     System.out.println("You first name is " + firstname + ", and your" + "\n" + "last name is " + lastname + "."); 
     System.out.println("WHAT A NICE NAME YOU HAVE!!"); 
     System.out.println(); //this is just so it looks nice with another space 
     int fnl = firstname.length(); 
     int lnl = lastname.length(); 
     System.out.println("Your first name countains " + fnl + " letters,"); 
     System.out.println("and your last name countains " + lnl + " letters."); 
     System.out.println(); //this is just so it looks nice with another space 
     char fni = firstname.charAt(0); 
     char lni = lastname.charAt(0); 
     System.out.println("Your initials are: " + fni + " " + lni); 
     System.out.println(); //this is just so it looks nice with another space 

    int acount = 0; int bcount = 0; int ccount = 0; int dcount = 0; int ecount = 0; int fcount = 0; int gcount = 0; 
    int hcount = 0; int icount = 0; int jcount = 0; int kcount = 0; int lcount = 0; int mcount = 0; int ncount = 0; 
    int ocount = 0; int pcount = 0; int qcount = 0; int rcount = 0; int scount = 0; int tcount = 0; int ucount = 0; 
    int vcount = 0; int wcount = 0; int xcount = 0; int ycount = 0; int zcount = 0; 

    for (int k = 0; ((k < fnl) && (k < lnl)); k++) 
    { 
     if (firstname.charAt(k) == ('a' | 'A')) acount++; 
     if (lastname.charAt(k) == ('a' | 'A')) acount++; 
     if (firstname.charAt(k) == ('b' | 'B')) bcount++; 
     if (lastname.charAt(k) == ('b' | 'B')) ccount++; 
     if (firstname.charAt(k) == ('c' | 'C')) ccount++; 
     if (lastname.charAt(k) == ('c' | 'C')) dcount++; 
     if (firstname.charAt(k) == ('d' | 'D')) dcount++; 
     if (lastname.charAt(k) == ('d' | 'D')) dcount++; 
     if (firstname.charAt(k) == ('e' | 'E')) ecount++; 
     if (lastname.charAt(k) == ('e' | 'E')) ecount++; 
     if (firstname.charAt(k) == ('f' | 'F')) fcount++; 
     if (lastname.charAt(k) == ('f' | 'F')) fcount++; 
     if (firstname.charAt(k) == ('g' | 'G')) gcount++; 
     if (lastname.charAt(k) == ('g' | 'G')) gcount++; 
     if (firstname.charAt(k) == ('h' | 'H')) hcount++; 
     if (lastname.charAt(k) == ('h' | 'H')) hcount++; 
     if (firstname.charAt(k) == ('i' | 'I')) icount++; 
     if (lastname.charAt(k) == ('i' | 'I')) icount++; 
     if (firstname.charAt(k) == ('j' | 'J')) jcount++; 
     if (lastname.charAt(k) == ('j' | 'J')) jcount++; 
     if (firstname.charAt(k) == ('k' | 'K')) kcount++; 
     if (lastname.charAt(k) == ('k' | 'K')) kcount++; 
     if (firstname.charAt(k) == ('l' | 'L')) lcount++; 
     if (lastname.charAt(k) == ('l' | 'L')) lcount++; 
     if (firstname.charAt(k) == ('m' | 'M')) mcount++; 
     if (lastname.charAt(k) == ('m' | 'M')) mcount++; 
     if (firstname.charAt(k) == ('n' | 'N')) ncount++; 
     if (lastname.charAt(k) == ('n' | 'N')) ncount++; 
     if (firstname.charAt(k) == ('o' | 'O')) ocount++; 
     if (lastname.charAt(k) == ('o' | 'O')) ocount++; 
     if (firstname.charAt(k) == ('p' | 'P')) pcount++; 
     if (lastname.charAt(k) == ('p' | 'P')) pcount++; 
     if (firstname.charAt(k) == ('q' | 'Q')) qcount++; 
     if (lastname.charAt(k) == ('q' | 'Q')) qcount++; 
     if (firstname.charAt(k) == ('r' | 'R')) rcount++; 
     if (lastname.charAt(k) == ('r' | 'R')) rcount++; 
     if (firstname.charAt(k) == ('s' | 'S')) scount++; 
     if (lastname.charAt(k) == ('s' | 'S')) scount++; 
     if (firstname.charAt(k) == ('t' | 'T')) tcount++; 
     if (lastname.charAt(k) == ('t' | 'T')) tcount++; 
     if (firstname.charAt(k) == ('u' | 'U')) ucount++; 
     if (lastname.charAt(k) == ('u' | 'U')) ucount++; 
     if (firstname.charAt(k) == ('v' | 'V')) vcount++; 
     if (lastname.charAt(k) == ('v' | 'V')) vcount++; 
     if (firstname.charAt(k) == ('w' | 'W')) wcount++; 
     if (lastname.charAt(k) == ('w' | 'W')) wcount++; 
     if (firstname.charAt(k) == ('x' | 'X')) xcount++; 
     if (lastname.charAt(k) == ('x' | 'X')) xcount++; 
     if (firstname.charAt(k) == ('y' | 'Y')) ycount++; 
     if (lastname.charAt(k) == ('y' | 'Y')) ycount++; 
     if (firstname.charAt(k) == ('z' | 'Z')) zcount++; 
     if (lastname.charAt(k) == ('z' | 'Z')) zcount++; 
    } 
    System.out.println(); //this is just so it looks nice with another space 
    System.out.println(firstname + " " + lastname + " contains " + acount + " a\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + bcount + " b\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + ccount + " c\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + dcount + " d\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + ecount + " e\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + fcount + " f\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + gcount + " g\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + hcount + " h\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + icount + " i\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + jcount + " j\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + kcount + " k\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + lcount + " l\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + mcount + " m\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + ncount + " n\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + ocount + " o\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + pcount + " p\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + qcount + " q\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + rcount + " r\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + scount + " s\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + tcount + " t\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + ucount + " u\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + vcount + " v\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + wcount + " y\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + xcount + " x\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + ycount + " y\'s"); 
    System.out.println(firstname + " " + lastname + " contains " + zcount + " z\'s"); 
} 

}

任何幫助將不勝感激。

我知道這很長,但它是我知道如何的唯一方法。

+0

如果您知道如何(或允許)使用'map',請使用它。這將使它更容易 – sam

+0

我不這樣做,因此不能使用它。 –

+0

對concat不熟悉,我嘗試了我知道的和出錯的等號,可能不得不花費更多時間來查看它。不過謝謝。 –

回答

0

這樣的事情應該改爲工作。不過,它仍然可以更簡單,寫得更好。

//create a new array with the number of possible ascii characters 
    int[] chars = new int[256]; 

    //loop through the length of first name 
    for(int i = 0; i<fnl; i++) 
    { 
     //Make lower case and save the char at index i and c 
     char c = firstname.toLowerCase().charAt(i); 
     //cast the saved char as a number and increment that position in the array 
     chars[(int) c]++; 
    } 

    //repeat for last name 
    for(int i = 0; i<lnl; i++) 
    { 
     char d = lastname.toLowerCase().charAt(i); 
     chars[(int) d]++; 
    } 

    //only want to print alphabetical letters a-z 
    for(char e = 'a'; e <= 'z'; e++) 
    { 
     //if the letter occurs 0 times, we start the loop at the next one 
     if (chars[(int) e]==0) 
      continue; 
     else 
      System.out.println("There are "+chars[(int) e]+" "+e+"'s in the String."); 
    } 
1

最簡單的辦法是使用一個26元素數組來表示的計數:

int[] letterCounts = new int(26); 

然後,您通過在名稱中的字母圈,像

for (char ch: (firstname+lastname).toLowerCase().toCharArray()) { 
} 

在體內該循環將char,ch轉換爲int,並減去97,這是a的ASCII代碼:

int c = ((int)ch)-97; 

這使用了一個事實,即字符真的只是一個整數 - (int)位'將'字符ch轉換爲int。然後用這個來解決數組:

letterCounts[c] = letterCounts + 1; 

循環之後,你應該循環一次,這次打印出來的數組內容和相關的字母,如:

for(int i = 0; i < 26; i++) { 
    System.out.print(((char)i+97)); 
    System.out.printLn(" " + letterCounts[i]); 
} 

在這裏,我們」重新使用一次強制轉換,但是這一次我們將一個int i + 97轉換回char,以便它可以被打印。我們已經添加了97個,因爲當i爲0時,將97添加到97中,當它是字符時,它是字母a

+0

這看起來像他希望我們做的,但他又沒有真正教導。他只是說去吧。不過謝謝。 –

+0

學習是一件積極的事情 - 當你被困住時,你已經做了正確的事情,通過放手去做。教學很難,因爲它很難理解,所以你應該給你的'老師'休息一下,其中大部分都是這樣,但他們只是在一個知之甚少的領域裏盡力而爲。 –

+0

此外,我們還沒有學會如何使用letterCounts或chararry –

相關問題