2015-10-26 30 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 –

相关问题