package lesson5;

import java.util.Scanner;

public class task1 {

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        String sentence ;
        System.out.println(" Please enter senctence with space: ");
        sentence = sc.nextLine();
        String words [] = sentence.split(" ");
        String mostconstword = null;
        char [] constants = {'b', 'c', 'd', 'f','g','j','h','q','x','z','l','m','p','s','r' , 'n', 't'};

        int maxconsant= 0;
        int totalconst = 0;

        for (String word : words){
            int conscem = 0;
            word=word.toLowerCase();

            for (int i = 0; i < word.length(); i++){
                char wr= word.charAt(i);
                conscem++;
                if (conscem > maxconsant || word.indexOf(wr) >= 0){
                    totalconst++;
                    maxconsant = conscem;
                    mostconstword = word;

                }

            }
        }
             System.out.println( "most constant word = " + mostconstword + " sum of constants in this word =" + totalconst);
    }

}


结果我想看:

我只是无法计算辅音的总和,其余的我就做完了。我需要帮助才能计算出辅音最多的作品中辅音的总和。

最佳答案

这是我的解决方案,解释在注释中

import java.util.Scanner;

public class task1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String sentence;
        System.out.println(" Please enter senctence with space: ");
        sentence = sc.nextLine();
        String words[] = sentence.split(" ");
        String mostconstword = null;
        char[] constants = {'b', 'c', 'd', 'f', 'g', 'j', 'h', 'q', 'x', 'z', 'l', 'm', 'p', 's', 'r', 'n', 't'};

        int maxconsonant = 0;

        //Each word splited
        for (String word : words) {
            int consonantInWord = 0;
            word = word.toLowerCase();
            //Each char in the current word
            for (int i = 0; i < word.length(); i++) {
                char wr = word.charAt(i);
                //Verify if consonant
                for (char constant : constants) {
                    if(wr == constant){
                        consonantInWord++;
                        break;
                    }
                }
            }
            //verify if maxconsonant
            if(maxconsonant < consonantInWord){
                mostconstword = word;
                maxconsonant = consonantInWord;
            }
        }
        System.out.println("most constant word = " + mostconstword + " sum of constants in this word =" + maxconsonant);
    }

}

10-07 19:23