检查是否有连续两个相同的元音。

huangapple go评论82阅读模式
英文:

Check for same vowel twice in a row

问题

//j - 显示连续出现的任何元音字母
System.out.print("\nj. ");

//必须为这些变量赋值
//否则默认语句将无法工作
char current_letter_1 = ' ', current_letter_2 = ' ';

//为这些字符创建字符串,以便更容易输出
String current_letter_1_string = String.valueOf(current_letter_1);
String current_letter_2_string = String.valueOf(current_letter_2);

//将其与小写句子进行比较,以忽略大小写
char low_letter = e.charAt(ind);

while (ind < a) {
    current_letter_1_string = String.valueOf(current_letter_1);
    current_letter_2_string = String.valueOf(current_letter_2);

    low_letter = e.charAt(ind);

    switch (low_letter) {
        case 'a', 'e', 'i', 'o', 'u', 'y':
            current_letter_1 = low_letter;
            break;
        default:
            break;
    }
    ind += 1;
    switch (low_letter) {
        case 'a', 'e', 'i', 'o', 'u', 'y':
            current_letter_2 = low_letter;
            break;
        default:
            break;
    }
    if (current_letter_1 == current_letter_2) {
        System.out.print(current_letter_2_string.trim() + " ");
    } else {
    }
    //以防同一个元音被连续重复超过两次
}
while (low_letter == current_letter_2 && ind < a) {
    low_letter = e.charAt(ind);
    ind += 1;
}
if (low_letter != current_letter_2) {
    ind += 1;
} else {
}

另外,下面是变量赋值部分的翻译:

Scanner sc(System.in);
String sentence = sc.nextLine();
int a = sentence.length();
String e = sentence.toLowerCase();
int ind = 0;

请注意,你提供的代码可能存在一些逻辑问题,但我只是根据你的要求进行翻译,不做代码逻辑上的修改。

英文:

I'm trying to write a program (really a section of a program) that checks to see if any vowels recur twice in a row.
For ex. input = boo, output = o

This is the code I have so far for this:

//j - displays any vowels which occur consecutively
System.out.print(&quot;\nj. &quot;);

// have to give these variables a value
// otherwise the default statements won&#39;t work
char current_letter_1 = &#39; &#39;, current_letter_2 = &#39; &#39;;

//making strings for these characters so they are easier to output
String current_letter_1_string = String.valueOf(current_letter_1);
String current_letter_2_string = String.valueOf(current_letter_2);

//checking it against the sentence in lowercase so it&#39;s case insensitive
char low_letter = e.charAt(ind);

while (ind &lt; a) {
    current_letter_1_string = String.valueOf(current_letter_1);
    current_letter_2_string = String.valueOf(current_letter_2);

    low_letter = e.charAt(ind);

    switch (low_letter) {
        case &#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;, &#39;y&#39;:
            current_letter_1 = low_letter;
            break;
        default:
            break;
    }
    ind += 1;
    switch (low_letter) {
        case &#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;, &#39;y&#39;:
            current_letter_2 = low_letter;
            break;
        default:
            break;
    }
    if (current_letter_1 == current_letter_2) {
        System.out.print(current_letter_2_string.trim() + &quot; &quot;);
    } else {
    }
    //in case the same vowel is repeated more than twice in a row
}
while (low_letter == current_letter_2 &amp;&amp; ind &lt; a) {
    low_letter = e.charAt(ind);
    ind += 1;
}
if (low_letter != current_letter_2) {
    ind += 1;
} else {
}

Btw since this is a subsections of a larger program I'll give you some variable values:

Scanner sc(System.in);
String sentence = sc.nextLine();
int a = sentence.length();
String e = sentence.toLowerCase();
int ind = 0;

Also, this is for school so if it looks really weird and you would do this completely differently, bear with me. This code sort of works, but the output is always weird, even with trim(), the variable is still outputted with space in front of it. Also, if sentence contains more than one repeated vowel, the output is screwed up again.
Any help is much appreciated.

答案1

得分: 1

保持简单

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入一个文本:");
        String sentence = sc.nextLine();

        // 转换为小写
        String sentenceLowerCase = sentence.toLowerCase();

        // 检查除最后一个字符外的字符是否与其后面的字符相同,并且是否为元音字母
        for (int i = 0; i < sentence.length() - 1; i++) {
            char ch = sentenceLowerCase.charAt(i);
            if (ch == sentenceLowerCase.charAt(i + 1)
                    && (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')) {
                System.out.println(ch);
            }
        }
    }
}

一个示例运行:

输入一个文本:Booster and rooster are different things
o
o

另一个示例运行:

输入一个文本:Faaster is a wrong spelling. Boost your vocabulary.
a
o


<details>
<summary>英文:</summary>
Keep it simple.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(&quot;Enter a text: &quot;);
String sentence = sc.nextLine();
// Text in the lower case
String sentenceLowerCase = sentence.toLowerCase();
// Check all but the last char if it is same as its following character and if
// it&#39;s a vowel
for (int i = 0; i &lt; sentence.length() - 1; i++) {
char ch = sentenceLowerCase.charAt(i);
if (ch == sentenceLowerCase.charAt(i + 1)
&amp;&amp; (ch == &#39;a&#39; || ch == &#39;e&#39; || ch == &#39;i&#39; || ch == &#39;o&#39; || ch == &#39;u&#39;)) {
System.out.println(ch);
}
}
}
}
**A sample run:**
Enter a text: Booster and rooster are different things
o
o
**Another sample run:**
Enter a text: Faaster is a wrong spelling. Boost your vocabulary.
a
o
</details>
# 答案2
**得分**: 0
```java
import java.util.Scanner;
// 也使用了 java.lang.StringBuilder 和 java.lang.Character
public class Ch6Extra1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String sentence;
do {
System.out.print("输入一个句子:");
sentence = sc.nextLine();
} while (sentence.equals(null) || sentence.equals(""));
// 在代码中使用的变量
int ind = 0;
char letter = sentence.charAt(ind);
// a - 字符计数器
int a;
a = sentence.length();
System.out.print("\na. " + a);
// b - 单词计数器
int b;
b = 0;
while (ind < a) {
letter = sentence.charAt(ind);
switch (letter) {
case ' ':
b += 1;
break;
default:
break;
}
ind += 1;
}
b += 1;
ind = 0;
System.out.print("\nb. " + b);
// c - 反转句子
StringBuilder c = new StringBuilder(sentence);
StringBuilder rev_sentence = c.reverse();
System.out.print("\nc. " + rev_sentence);
// d - 句子转大写
String d;
d = sentence.toUpperCase();
System.out.print("\nd. " + d);
// e - 句子转小写
String e;
e = sentence.toLowerCase();
System.out.print("\ne. " + e);
// f、g 和 m - 元音、辅音和标点符号计数
int f, g, m;
f = 0;
g = 0;
m = 0;
ind = 0;
while (ind < a) {
letter = sentence.charAt(ind);
switch (letter) {
case 'a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y':
f += 1;
break;
// 如果是数字,代码会继续到下一个字符
case '1', '2', '3', '4', '5', '6', '7', '8', '9':
break;
// 如果是特殊字符,代码会继续到下一个字符
case 'b', 'B', 'c', 'C', 'd', 'D', 'f', 'F', 'g', 'G', 'h', 'H',
'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'p', 'P',
'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'v', 'V', 'w', 'W',
'x', 'X', 'z', 'Z':
g += 1;
break;
// 检查标点符号
case '.', ',', ';', ':', '?', '!':
m += 1;
break;
default:
break;
}
ind += 1;
}
System.out.print("\nf. " + f);
System.out.print("\ng. " + g);
ind = 0;
// h - 第一个单词的 ASCII 码
System.out.print("\nh. ");
while (ind < a) {
letter = sentence.charAt(ind);
switch (letter) {
case ' ':
ind = a - 1;
break;
default:
System.out.print((int) letter + " ");
break;
}
ind += 1;
}
ind = 0;
// i - 检查句子中是否有单词 "and"
boolean i = e.contains("and");
if (i == true) {
System.out.print("\ni. 是");
} else if (i == false) {
System.out.print("\ni. 否");
}
// j - 显示连续出现的元音字母
System.out.print("\nj. ");
// 必须为这些变量赋值,否则默认语句将无法工作
char current_letter_1 = ' ', current_letter_2 = ' ';
// 为这些字符创建字符串,以便更容易输出
String current_letter_1_string = String.valueOf(current_letter_1);
String current_letter_2_string = String.valueOf(current_letter_2);
// 在小写的句子中检查,以便大小写不敏感
char low_letter = e.charAt(ind);
while (ind < a) {
current_letter_1_string = String.valueOf(current_letter_1);
current_letter_2_string = String.valueOf(current_letter_2);
low_letter = e.charAt(ind);
switch (low_letter) {
case 'a', 'e', 'i', 'o', 'u', 'y':
current_letter_1 = low_letter;
break;
default:
break;
}
ind += 1;
switch (low_letter) {
case 'a', 'e', 'i', 'o', 'u', 'y':
current_letter_2 = low_letter;
break;
default:
break;
}
if (current_letter_1 == current_letter_2) {
System.out.print(current_letter_2_string.trim() + " ");
}
// 如果相同的元音连续重复超过两次
while (low_letter == current_letter_2 && ind < a) {
low_letter = e.charAt(ind);
ind += 1;
}
}
// k 和 l 一起 - 大写和小写计数器
int k, l;
k = 0;
l = 0;
boolean up_case, low_case;
while (ind < a) {
up_case = Character.isUpperCase(letter);
low_case = Character.isLowerCase(letter);
letter = sentence.charAt(ind);
if (up_case == true) {
k += 1;
} else if (up_case == false) {
if (low_case == true) {
l += 1;
}
}
ind += 1;
}
System.out.print("\nk. " + k);
System.out.print("\nl. " + l);
System.out.print("\nm. " + m);
System.out.println("\n");
}
}
英文:

Here is the full code:

import java.util.Scanner;
//also using java.lang.StringBuilder and java.lang.Character
public class Ch6Extra1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String sentence;
do {
System.out.print(&quot;Enter a sentence: &quot;);
sentence = sc.nextLine();
} while (sentence.equals(null) || sentence.equals(&quot;&quot;));
//variables used throughout code
int ind = 0;
char letter = sentence.charAt(ind);
//a - character counter
int a;
a = sentence.length();
System.out.print(&quot;\na. &quot; + a);
//b - word counter
int b;
b = 0;
while (ind &lt; a) {
letter = sentence.charAt(ind);
switch (letter) {
case &#39; &#39;:
b += 1;
break;
default:
break;
}
ind += 1;
}
b += 1;
ind = 0;
System.out.print(&quot;\nb. &quot; + b);
//c - sentence reversed
StringBuilder c = new StringBuilder(sentence);
StringBuilder rev_sentence = c.reverse();
System.out.print(&quot;\nc. &quot; + rev_sentence);
//d - sentence to uppercase
String d;
d = sentence.toUpperCase();
System.out.print(&quot;\nd. &quot; + d);
//e - sentence to lowercase
String e;
e = sentence.toLowerCase();
System.out.print(&quot;\ne. &quot; + e);
//f, g and m - vowel, consonant, and punctuation count
int f, g, m;
f = 0;
g = 0;
m = 0;
ind = 0;
while (ind &lt; a) {
letter = sentence.charAt(ind);
switch (letter) {
case &#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;, &#39;y&#39;, &#39;A&#39;, &#39;E&#39;, &#39;I&#39;, &#39;O&#39;, &#39;U&#39;, &#39;Y&#39;:
f += 1;
break;
//in case it&#39;s a number, the code will pass to the next char
case 1, 2, 3, 4, 5, 6, 7, 8, 9:
break;
//in case it&#39;s a special character, the code will pass to the next char
case &#39;b&#39;, &#39;B&#39;, &#39;c&#39;, &#39;C&#39;, &#39;d&#39;, &#39;D&#39;, &#39;f&#39;, &#39;F&#39;, &#39;g&#39;, &#39;G&#39;, &#39;h&#39;, &#39;H&#39;, 
&#39;j&#39;, &#39;J&#39;, &#39;k&#39;, &#39;K&#39;, &#39;l&#39;, &#39;L&#39;, &#39;m&#39;, &#39;M&#39;, &#39;n&#39;, &#39;N&#39;, &#39;p&#39;, &#39;P&#39;, 
&#39;q&#39;, &#39;Q&#39;, &#39;r&#39;, &#39;R&#39;, &#39;s&#39;, &#39;S&#39;, &#39;t&#39;, &#39;T&#39;, &#39;v&#39;, &#39;V&#39;, &#39;w&#39;, &#39;W&#39;, 
&#39;x&#39;, &#39;X&#39;, &#39;z&#39;, &#39;Z&#39;:
g += 1;
break;
//checks for punctuation
case &#39;.&#39;, &#39;,&#39;, &#39;;&#39;, &#39;:&#39;, &#39;?&#39;, &#39;!&#39;:
m += 1;
break;
default:
break;
}
ind += 1;
}
System.out.print(&quot;\nf. &quot; + f);
System.out.print(&quot;\ng. &quot; + g);
ind = 0;
//h - ASCII of first word
System.out.print(&quot;\nh. &quot;);
while (ind &lt; a) {
letter = sentence.charAt(ind);
switch (letter) {
case &#39; &#39;:
ind = a -1;
break;
default:
System.out.print((int)letter + &quot; &quot;);
break;
}
ind += 1;
}
ind = 0;
//i - checks for word and in the sentence
boolean i = e.contains(&quot;and&quot;);
if (i == true) {
System.out.print(&quot;\ni. Yes&quot;);
} else if (i == false) {
System.out.print(&quot;\ni. No&quot;);
}
//j - displays any vowels which occur consecutively
System.out.print(&quot;\nj. &quot;);
//have to give these variables a value otherwise the default statements won&#39;t work
char current_letter_1 = &#39; &#39;, current_letter_2 = &#39; &#39;;
//making strings for these characters so they are easier to output
String current_letter_1_string  = String.valueOf(current_letter_1);
String current_letter_2_string  = String.valueOf(current_letter_2);
//checking it against the sentence in lowercase so it&#39;s case insensitive
char low_letter = e.charAt(ind);
while (ind &lt; a) {
current_letter_1_string  = String.valueOf(current_letter_1);
current_letter_2_string  = String.valueOf(current_letter_2);
low_letter = e.charAt(ind);
switch (low_letter) {
case &#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;, &#39;y&#39;:
current_letter_1 = low_letter;
break;
default:
break;
}
ind += 1;
switch (low_letter) {
case &#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;, &#39;y&#39;:
current_letter_2 = low_letter;
break;
default:
break;
} if (current_letter_1 == current_letter_2) {
System.out.print(current_letter_2_string.trim() + &quot; &quot;);
} else {
}
//in case the same vowel is repeated more than twice in a row
} while (low_letter == current_letter_2 &amp;&amp; ind &lt; a){
low_letter = e.charAt(ind);
ind += 1;
} if (low_letter != current_letter_2) {
ind += 1;
} else {
}
//k and l together - Upper and Lower case counters
int k, l;
k = 0;
l = 0;
boolean up_case = Character.isUpperCase(letter);
boolean low_case = Character.isUpperCase(letter);
while (ind &lt; a) {
up_case = Character.isUpperCase(letter);
low_case = Character.isLowerCase(letter);
letter = sentence.charAt(ind);
if (up_case == true) {
k += 1;
} else if (up_case == false) {
if (low_case == true) {
l += 1;
} else {
}
} else {
}
ind += 1;
}
System.out.print(&quot;\nk. &quot; + k);
System.out.print(&quot;\nl. &quot; + l);
System.out.print(&quot;\nm. &quot; + m);
System.out.println(&quot;\n&quot;);
}
}

I see someone posted an answer. I'll try that and checkmark it if it works.

答案3

得分: 0

另一种可能性是使用正则表达式(这是你在继续学习Java时可能希望熟悉的内容)。

测试数据

String[] data =
		{ "boo", "hello", "vacuUm", "keenly", "weedprOof" };

模式。它查找任何后面跟着相同元音字母的元音字母。

  • (?i:) 忽略匹配中的大小写。
  • ([aeiou]) 是一个元音字母捕获组。
  • (?=\\1) 是一个用于在捕获块中从先前匹配的元音字母之后查找的零宽度前瞻(所有这些在上面的链接中都有解释)。
Pattern p = Pattern.compile("(?i:)([aeiou])(?=\\1)");

现在只需遍历单词,查找每个单词的一个或多个模式匹配。这会打印每个找到的一对字母,包括像eee这样的模式,它将是两对e

for (String d : data) {
	Matcher m = p.matcher(d);
	System.out.printf("%-10s -> ", d);
	boolean match = false;
	while (m.find()) {
		match = true;
		System.out.print(m.group(1) + " ");
	}
	System.out.println(match ? "" : "none found.");
}

或者非正则表达式方法

String[] data =
		{ "boo", "hello", "vacuUm", "keenly", "weedprOof" };

for (String d : data) {
	String orig = d;
	char[] chs = d.toLowerCase().toCharArray();
	System.out.printf("%-10s -> ", orig);
	boolean match = false;
	for (int i = 0; i < chs.length - 1; i++) {
		if ("aeiou".indexOf(chs[i]) >= 0) {
			if (chs[i] == chs[i + 1]) {
				match = true;
				System.out.print(chs[i] + " ");
			}
		}
	}
	System.out.println(match ? "" : "none found.");
}

两者都会打印

boo        -> o 
hello      -> none found.
vacuUm     -> u 
keenly     -> e 
weedprOof  -> e o 
英文:

Another possibility is to use regular expressions (something that you may want to get familiar with as you continue to learn Java).

Test Data

String[] data =
{ &quot;boo&quot;, &quot;hello&quot;, &quot;vacuUm&quot;, &quot;keenly&quot;, &quot;weedprOof&quot; };

The pattern. It looks for any vowel followed by the same vowel.

  • the (?i:) ignores case in matches.
  • the ([aeiou]) is a vowel capture group
  • the (?=\\1) is a zero width lookahead for the previously matched vowel from
    the capture block (all this is explained in the link above).
Pattern p = Pattern.compile(&quot;(?i:)([aeiou])(?=\\1)&quot;);

Now just iterate over the words, looking for one or more pattern matches for each word. This prints one letter for each pair found, including patterns like eee which would be two pair of e&#39;s

for (String d : data) {
Matcher m = p.matcher(d);
System.out.printf(&quot;%-10s -&gt; &quot;, d);
boolean match = false;
while (m.find()) {
match = true;
System.out.print(m.group(1) + &quot; &quot;);
}
System.out.println(match ? &quot;&quot; : &quot;none found.&quot;);
}

Or the non regex approach

String[] data =
{ &quot;boo&quot;, &quot;hello&quot;, &quot;vacuUm&quot;, &quot;keenly&quot;, &quot;weedprOof&quot; };
for (String d : data) {
String orig = d;
char[] chs = d.toLowerCase().toCharArray();
System.out.printf(&quot;%-10s -&gt; &quot;, orig);
boolean match = false;
for (int i = 0; i &lt; chs.length - 1; i++) {
if (&quot;aeiou&quot;.indexOf(chs[i]) &gt;= 0) {
if (chs[i] == chs[i + 1]) {
match = true;
System.out.print(chs[i] + &quot; &quot;);
}
}
}
System.out.println(match ? &quot;&quot; : &quot;none found.&quot;);
}

Both print

boo        -&gt; o 
hello      -&gt; none found.
vacuUm     -&gt; u 
keenly     -&gt; e 
weedprOof  -&gt; e o 
</details>

huangapple
  • 本文由 发表于 2020年10月2日 23:03:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64173860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定