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);
}
}
结果我想看:
我无法计算辅音的总和,其余的我已经完成了。我需要帮助才能用最辅音来计算辅音的总和。
答案 0 :(得分:2)
你没有检查条件中的辅音。这就是为什么你的辅音数量错误的原因。
您可以使用vowel
代替consonant
进行检查。这将需要更少的检查。
Scanner sc = new Scanner(System.in);
String sentence;
System.out.println(" Please enter senctence with space: ");
sentence = sc.nextLine();
String words[] = sentence.split(" ");
String maxConsonantWord = null;
int maxConsonant = 0;
for (String word : words) {
int countOfConsonants = 0;
word = word.toLowerCase();
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if(ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') // Not vowel, that means consonant
countOfConsonants++;
}
if(countOfConsonants > maxConsonant) {
maxConsonant = countOfConsonants;
maxConsonantWord = word;
}
}
System.out.println("Most consonant word = " + maxConsonantWord + "\nNumber of consonant in this word =" + maxConsonant);
答案 1 :(得分:1)
你根本没有任何代码来分析你的话语中的字符 - 截至目前,你正在增加你的&#34;辅音&#34;计数器中的每个字符和任何字符。换句话说:你错过了实际决定的代码&#34;这个字符是辅音&#34;。
你得到了:
if (conscem > maxconsant || word.indexOf(wr) >= 0){
根本没有任何帮助,因为这是一个辅音&#34;!
之前你定义了一个数组常量 - 但你没有任何代码可以检查单词中的字符是否是辅音!
这样做的一种方法是这样:
Set<Character> consonants = new HashSet<>(Arrays.asList('b', 'c', 'd', ...));
(这里指出:一个集合是一个更好的数据结构,用于检查字符是否在其中 - 如果您将使用您的数组,您总是必须迭代整个数组以确定字符是否在该数组中!)
...在你的内循环中使用它:
char wr= word.charAt(i);
if (consonants.contains(wr)) {
// its a consonant
} else {
// it is something else
}
答案 2 :(得分:1)
这是我的解决方案,解释在评论中
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);
}
}
答案 3 :(得分:1)
如果您的代码返回了正确的最常量单词,那么您可以添加以下代码段来计算该单词中常量的数量
int totalconstword = 0;
for (int i = 0; i < mostconstword.length(); i++){
if(Arrays.binarySearch(constants, mostconstword.charAt(i))>=0){
totalconstword++;
}
}
System.out.println( "most constant word = " + mostconstword + " sum of constants in this word =" + totalconstword);