如何提取所有以元音开头且长度等于 n(在 JAVA 中)的单词?
How do I extract all the words start with a vowel and length equal with n (in JAVA)?
如何从单词长度为 3(或用户输入的另一个数字)且以元音开头的句子中提取单词?
public class LAB1 {
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.print("Introduceți textul: ");
String s=br.readLine();
s = s+" ";
int l=s.length();
int pos=0;
char ch1, ch2;
String w;
for(int i=0; i<l; i++)
{
ch1 = s.charAt(i);
if(ch1 == ' ')
{
w = s.substring(pos,i); // extracting words one by one
ch2 = w.charAt(0);
if(ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U' ||
ch2=='a' || ch2=='e' || ch2=='i' || ch2=='o' || ch2=='u')
{
System.out.println(w);
}
pos = i+1;
}
}
}
}
运行:
Introducei textul: Sprin is Beautiful for Ana
是
安娜
组装成功(总时间:33 秒)
看看 Regex(正则表达式)。它们正是为此类问题而设计的,可以在 Java 中本地使用。
部分教程:https://www.vogella.com/tutorials/JavaRegularExpressions/article.html
为了测试你的正则表达式,这是一个方便的网站https://regexr.com/
我同意正则表达式可以提供帮助,所以可以这样做:
if (word.matches("(?i)^[aeiou].{2}")) { ... }
调整“2”以匹配所需长度 - 1,并且 "aeiou" 可以扩展以支持其他元音。
不过,这样的做法也是相当先进的。对于更基本的方法,我会考虑将问题分为两种不同的方法,并使用 switch
而不是带有很多 ||
子句的复杂 if
语句。
public static void main(String[] args)
{
// you can gather these entries from a Scanner, or whatever
final String inp = "Spring is Amazingly Beautiful for Ana";
final int len = 3;
String[] words = extractWords(inp);
for (String word : words) {
if (correctLength(word, len) && startsWithVowel(word)) {
System.out.println(word);
}
}
}
public static String[] extractWords(final String sentence)
{
return sentence.split("[\s]+");
}
public static boolean correctLength(String word, int expLen)
{
return word.length() == expLen;
}
public static boolean startsWithVowel(final String word)
{
if (word == null || word.isBlank()) {
return false;
}
boolean startsWith = false;
// really need to develop a comprehensive approach to what is a vowel
// could use regular expressions,
//
// return word.matches("(?i)^[aeiou].*$");
//
// but it is slightly easier if
// just use a known set since there are more than aeiou in the world
// also, we will set to lower case, so can use smaller set
switch (word.toLowerCase().charAt(0)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
startsWith = true;
break;
default:
break;
}
return startsWith;
}
这种方法将词长和词首的问题分离到不同的方法中(也支持更简单的测试),并使用.split()
来获取词。
如何从单词长度为 3(或用户输入的另一个数字)且以元音开头的句子中提取单词?
public class LAB1 {
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.print("Introduceți textul: ");
String s=br.readLine();
s = s+" ";
int l=s.length();
int pos=0;
char ch1, ch2;
String w;
for(int i=0; i<l; i++)
{
ch1 = s.charAt(i);
if(ch1 == ' ')
{
w = s.substring(pos,i); // extracting words one by one
ch2 = w.charAt(0);
if(ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U' ||
ch2=='a' || ch2=='e' || ch2=='i' || ch2=='o' || ch2=='u')
{
System.out.println(w);
}
pos = i+1;
}
}
}
}
运行: Introducei textul: Sprin is Beautiful for Ana 是 安娜 组装成功(总时间:33 秒)
看看 Regex(正则表达式)。它们正是为此类问题而设计的,可以在 Java 中本地使用。
部分教程:https://www.vogella.com/tutorials/JavaRegularExpressions/article.html
为了测试你的正则表达式,这是一个方便的网站https://regexr.com/
我同意正则表达式可以提供帮助,所以可以这样做:
if (word.matches("(?i)^[aeiou].{2}")) { ... }
调整“2”以匹配所需长度 - 1,并且 "aeiou" 可以扩展以支持其他元音。
不过,这样的做法也是相当先进的。对于更基本的方法,我会考虑将问题分为两种不同的方法,并使用 switch
而不是带有很多 ||
子句的复杂 if
语句。
public static void main(String[] args)
{
// you can gather these entries from a Scanner, or whatever
final String inp = "Spring is Amazingly Beautiful for Ana";
final int len = 3;
String[] words = extractWords(inp);
for (String word : words) {
if (correctLength(word, len) && startsWithVowel(word)) {
System.out.println(word);
}
}
}
public static String[] extractWords(final String sentence)
{
return sentence.split("[\s]+");
}
public static boolean correctLength(String word, int expLen)
{
return word.length() == expLen;
}
public static boolean startsWithVowel(final String word)
{
if (word == null || word.isBlank()) {
return false;
}
boolean startsWith = false;
// really need to develop a comprehensive approach to what is a vowel
// could use regular expressions,
//
// return word.matches("(?i)^[aeiou].*$");
//
// but it is slightly easier if
// just use a known set since there are more than aeiou in the world
// also, we will set to lower case, so can use smaller set
switch (word.toLowerCase().charAt(0)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
startsWith = true;
break;
default:
break;
}
return startsWith;
}
这种方法将词长和词首的问题分离到不同的方法中(也支持更简单的测试),并使用.split()
来获取词。