查找辅音数量最多的单词和该单词的辅音总和 package第五课; 导入java.util.Scanner; 公共课任务1{ 公共静态void main(字符串[]args){ 扫描仪sc=新的扫描仪(System.in); 串句; System.out.println(“请用空格输入senctence:”; 句子=sc.nextLine(); 字符串单词[]=句子。拆分(“”); 字符串mostconstword=null; char[]常数={b',c',d',f',g',j',h',q',x',z',l',m',p',s',r',n',t'}; int-maxconsant=0; int totalconst=0; for(字符串字:字){ int conscem=0; word=word.toLowerCase(); for(int i=0;imaxconsant | | word.indexOf(wr)>=0){ totalconst++; maxconsant=conscem; mostconstword=单词; } } } System.out.println(“most常量字=“+mostconstword+”此字常量之和=“+totalconst”); } }

查找辅音数量最多的单词和该单词的辅音总和 package第五课; 导入java.util.Scanner; 公共课任务1{ 公共静态void main(字符串[]args){ 扫描仪sc=新的扫描仪(System.in); 串句; System.out.println(“请用空格输入senctence:”; 句子=sc.nextLine(); 字符串单词[]=句子。拆分(“”); 字符串mostconstword=null; char[]常数={b',c',d',f',g',j',h',q',x',z',l',m',p',s',r',n',t'}; int-maxconsant=0; int totalconst=0; for(字符串字:字){ int conscem=0; word=word.toLowerCase(); for(int i=0;imaxconsant | | word.indexOf(wr)>=0){ totalconst++; maxconsant=conscem; mostconstword=单词; } } } System.out.println(“most常量字=“+mostconstword+”此字常量之和=“+totalconst”); } },java,Java,我想看到的结果是: 我只是不能计算辅音的和,其余的我都做了。我需要的帮助只是计算工作中使用最多辅音的辅音总和。您的are根本没有任何代码来分析单词中的字符-到目前为止,您正在为单词中的每个字符增加“辅音”计数器。换句话说,您缺少了真正决定“这个字符是辅音”的代码 你得到: package lesson5; import java.util.Scanner; public class task1 { public static void main(String[] args) {

我想看到的结果是:


我只是不能计算辅音的和,其余的我都做了。我需要的帮助只是计算工作中使用最多辅音的辅音总和。

您的are根本没有任何代码来分析单词中的字符-到目前为止,您正在为单词中的每个字符增加“辅音”计数器。换句话说,您缺少了真正决定“这个字符是辅音”的代码

你得到:

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);
    }

}
这对计算“wr是辅音”一点帮助都没有

您之前定义了一个数组常量,但是没有任何代码可以检查word中的字符是否是辅音

一种方法是这样做:

if (conscem > maxconsant || word.indexOf(wr) >= 0){

你的are根本没有任何代码来分析单词中的字符-到目前为止,你正在增加单词中每个字符的“辅音”计数器。换句话说,您缺少了真正决定“这个字符是辅音”的代码

你得到:

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);
    }

}
这对计算“wr是辅音”一点帮助都没有

您之前定义了一个数组常量,但是没有任何代码可以检查word中的字符是否是辅音

一种方法是这样做:

if (conscem > maxconsant || word.indexOf(wr) >= 0){

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

char wr= word.charAt(i);
if (consonants.contains(wr)) {
   // its a consonant
} else {
   // it is something else
}
import java.util.Scanner;
公共课任务1{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
串句;
System.out.println(“请用空格输入senctence:”;
句子=sc.nextLine();
字符串单词[]=句子。拆分(“”);
字符串mostconstword=null;
char[]常量={'b','c','d','f','g','j','h','q','x','z','l','m','p','s','r','n','t'};
int maxconsonant=0;
//每个字都分裂了
for(字符串字:字){
int辅音inword=0;
word=word.toLowerCase();
//当前单词中的每个字符
for(int i=0;i
这是我的解决方案,说明在注释中

char wr= word.charAt(i);
if (consonants.contains(wr)) {
   // its a consonant
} else {
   // it is something else
}
import java.util.Scanner;
公共课任务1{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
串句;
System.out.println(“请用空格输入senctence:”;
句子=sc.nextLine();
字符串单词[]=句子。拆分(“”);
字符串mostconstword=null;
char[]常量={'b','c','d','f','g','j','h','q','x','z','l','m','p','s','r','n','t'};
int maxconsonant=0;
//每个字都分裂了
for(字符串字:字){
int辅音inword=0;
word=word.toLowerCase();
//当前单词中的每个字符
for(int i=0;i
如果您的代码返回了正确的mostconstant单词,那么您可以添加以下代码段来计算该单词中的常量数

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);
    }

}
int totalconstword=0;
对于(int i=0;i=0){
totalconstword++;
}
}
System.out.println(“most constant word=“+MOSTCConstantWord+”此word中常量之和=“+TotalConstantWord”);

如果您的代码返回了正确的mostconstant单词,那么您可以添加