import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
System.out.println(input);
String[] sentences = input.split("\\. ");
char[] reversedText = input.toCharArray();
char[] normalizedText = input.toCharArray();
int sentenceCount = 0;
int vowelCount = 0;
int consonantCount = 0;
for (String sentence : sentences) {
vowelCount = 0;
consonantCount = 0;
sentenceCount++;
System.out.println(sentence);
String[] words = sentence.split(" ");
System.out.println("Number of words is " + words.length);
String lowerCase = sentence.toLowerCase();
char[] chars = lowerCase.toCharArray();
System.out.println("Number of characters is " + sentence.length());
for (char c : chars) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y') {
vowelCount++;
} else if (c == 'h' || c == 'k' || c == 'r' || c == 'd' || c == 't' || c == 'n') {
consonantCount++;
}
}
System.out.println("Number of vowels is " + vowelCount);
System.out.println("Number of consonants is " + consonantCount);
}
System.out.println(" ");
System.out.println("Number of sentences is " + sentenceCount);
for (int i = (reversedText.length) - 1; i >= 0; i--) {
System.out.print(reversedText[i]);
}
System.out.println(" ");
char[] diacritics = {'ě', 'š', 'č', 'ř', 'ž', 'ý', 'á', 'í', 'é', 'ú', 'ů', 'Ě', 'Š', 'Č', 'Ř', 'Ž', 'Ý', 'Á', 'Í', 'É', 'Ú', 'Ů' };
char[] normal = {'e', 's', 'c', 'r', 'z', 'y', 'a', 'i', 'e', 'u', 'u', 'E', 'S', 'C', 'R', 'Z', 'Y', 'A', 'I', 'E', 'U', 'U' };
for (int i = 0; i < normalizedText.length; i++) {
char c = normalizedText[i];
int index = -1;
for (int j = 0; j < diacritics.length; j++) {
if (c == diacritics[j]) {
index = j;
break;
}
}
if (index != -1) {
normalizedText[i] = normal[index];
}
}
System.out.println(normalizedText);
}}
我正在使用 Strings。 有人可以告诉我如何用一种方法做到这一点吗?也请指出我的代码一般有问题的地方。
我试图用“普通”文本替换所有变音符号,这是我想出的,但这不是一种方法,而是手动完成的。
还有很多事情要做,但下面的代码可以让您对如何创建各种方法有一个基本的了解。
我从原始代码中分离出两个方法。您仍然可以改进它,首先也是最重要的是继续创建一个
private static void printReversed()
方法来包含反向文本打印代码。接下来,稍微更高级和更复杂的是让所有方法返回一些东西而不是直接打印出东西(就像 removeDiacritics()
那样)。在编程中,我们通常不喜欢有“副作用”(如打印)的方法,而更喜欢接受一些东西、转换它并返回一些东西的纯方法,这样调用代码就可以用它做任何它想做的事情。喜欢打印 :).
public class Main {
public static void main(String... args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
System.out.println(input);
System.out.println();
printStatistics(input);
char[] reversedText = input.toCharArray();
for (int i = (reversedText.length) - 1; i >= 0; i--) {
System.out.print(reversedText[i]);
}
System.out.println();
String normalized = removeDiacritics(input);
System.out.println(normalized);
}
private static void printStatistics(String input) {
String[] sentences = input.split("\\. ");
int sentenceCount = 0;
int vowelCount = 0;
int consonantCount = 0;
for (String sentence : sentences) {
vowelCount = 0;
consonantCount = 0;
sentenceCount++;
System.out.println(sentence);
String[] words = sentence.split(" ");
System.out.println("Number of words is " + words.length);
String lowerCase = sentence.toLowerCase();
char[] chars = lowerCase.toCharArray();
System.out.println("Number of characters is " + sentence.length());
for (char c : chars) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y') {
vowelCount++;
} else if (c == 'h' || c == 'k' || c == 'r' || c == 'd' || c == 't' || c == 'n') {
consonantCount++;
}
}
System.out.println("Number of vowels is " + vowelCount);
System.out.println("Number of consonants is " + consonantCount);
}
System.out.println();
System.out.println("Number of sentences is " + sentenceCount);
System.out.println();
}
private static String removeDiacritics(String textWithDiacritics) {
char[] normalizedText = textWithDiacritics.toCharArray();
char[] diacritics = { 'ě', 'š', 'č', 'ř', 'ž', 'ý', 'á', 'í', 'é', 'ú', 'ů', 'Ě', 'Š', 'Č', 'Ř', 'Ž', 'Ý', 'Á', 'Í', 'É', 'Ú', 'Ů' };
char[] normal = { 'e', 's', 'c', 'r', 'z', 'y', 'a', 'i', 'e', 'u', 'u', 'E', 'S', 'C', 'R', 'Z', 'Y', 'A', 'I', 'E', 'U', 'U' };
for (int i = 0; i < normalizedText.length; i++) {
char c = normalizedText[i];
int index = -1;
for (int j = 0; j < diacritics.length; j++) {
if (c == diacritics[j]) {
index = j;
break;
}
}
if (index != -1) {
normalizedText[i] = normal[index];
}
}
return String.valueOf(normalizedText);
}
}