Java Couting Charcters使用方法

时间:2015-12-03 18:42:26

标签: java

我试图让第一个for循环进入它自己的私有方法,我似乎无法得到它而不会搞砸输入。目标只是让main方法用于调用方法,并在方法中完成所有实际计算。

任何指导都将不胜感激

这是我的代码的一部分:

import java.util.Arrays;
import java.util.Scanner;

public class CountChars {
static int[] alphabetArray = new int[26];
public static void main(String[] args) {
     int linenum = 0;
        Scanner input = new Scanner(System.in);
        while(input.hasNext()){
          String userInput =input.nextLine();
          String input1 = userInput.toLowerCase();
          for ( int i = 0; i < input1.length(); i++ ) {
             char ch=  input1.charAt(i);
             int value = (int) ch;
             if (value >= 97 && value <= 122){
             alphabetArray[ch-'a']++;
          }
        }
     int others= counts(++linenum, userInput);
    printLetterCounts();
    String yesorno= anyVowels(userInput);
    String VowCont= countVowels (userInput);
    int Contcount= countConsonants(0, userInput);
    zeroLetterCount();
    System.out.print(" others="+others); // ADDED THIS LINE
    System.out.println();    

2 个答案:

答案 0 :(得分:1)

执行此操作也不正确/没有任何意义:

private static void zeroLetterCount(){
     for (int i=0;i<alphabetArray.length;i++)
          alphabetArray[i] = 0;
}

更改计数方法:

private static int counts(int lineNum, String s){
     int count0 = s.length();
     int count1 = s.replaceAll("[^aeiou]","").length();
     int count2 = s.replaceAll("[^bcdfghjklmnpqrstvwxyz]","").length();
     int calc = count0-count1-count2;
     System.out.printf("Line %02d:", lineNum);
     return calc;
 }

将您改为:

public static void main(String[] args) {
     int linenum = 0;
        Scanner input = new Scanner(System.in);
        while(input.hasNext()){
          String userInput =input.nextLine();
          String input1 = userInput.toLowerCase();
          for ( int i = 0; i < input1.length(); i++ ) {
             char ch=  input1.charAt(i);
             int value = (int) ch;
             if (value >= 97 && value <= 122){
             alphabetArray[ch-'a']++;
          }
        }
     int others= counts(++linenum, userInput); // THIS LINE CHANGE AND MOVED UP.
    countAllChars();
    String yesorno= anyVowels(userInput);
    String VowCont= countVowels (userInput);
    int Contcount= countConsonants(0, userInput);
    zeroLetterCount();
    System.out.print(" others="+others); // ADDED THIS LINE
    System.out.println();    
   }
}

计算每个char方法:

private static void countAllChars() {
    for (int i = 0; i < alphabetArray.length; i++) {
        if(alphabetArray[i]>0) {
            char ch = (char) (i+97);
            System.out.print(ch +"="+alphabetArray[i]+ " ");
        }
    }
}

INPUT:

hevdhfewfewfe
hhwwrrr73##$6%&%7

输出:

Line 01:d=1 e=4 f=3 h=2 v=1 w=2 YES vowels=4 Consonants=9 others=0
Line 02:h=2 r=3 w=2 NO vowels=0 Consonants=7 others=10

答案 1 :(得分:0)

如果你需要一个方法中的行计数器,你可以这样做:

   linenum = printLineCount(linenum);

[...]
   protected int printLineCount(int lineCount) {
     System.out.printf("Line %02d:", lineCount);
     return lineCount + 1;
   }

与此同时,您唯一的换行System.out.println()位于while循环之外。应该在内,因此每行输入创建一行。

第二个想法:你想要的输出决定你先处理所有输入然后再创建输出。那么也许您应该首先将输入行填充到List中并对其进行操作以生成输出?