Java 交换字符但不交换元音
Java swap characters but not vowels
我想交换字符串中除元音字母以外的字符。那里有很多代码,但是,这是我正在处理的代码,我发现它很容易理解,但这并没有产生预期的结果。
public class RandomPractise {
//1st find out a vowel
public Boolean isVowel(char c) {
boolean isV = true;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
isV = true;
} else {
isV = false;
}
return isV;
}
//Check current and next chars if not vowel do the swap
public void randomTest() {
String str = "boat";
char[] c1 = str.toCharArray();
for (int i = 0; i < c1.length - 1; i++) {
if (!(isVowel(c1[i])) && !(isVowel(c1[i + 1]))) {
char temp = c1[i];
c1[i] = c1[i + 1];
c1[i + 1] = temp;
}
}
System.out.println(String.valueOf(c1));
}
public static void main(String[] args) {
RandomPractise r = new RandomPractise();
r.randomTest();
}
示例:如果我使用输入:boat,我希望看到输出:toab [这不会发生]。当我使用输入时:sboath 我看到输出 bsoaht。
问题:我应该做些什么改变才能使其适用于船?
提前感谢您的宝贵时间。
"Question: What change should I do to make it work for boat?"
你是要这样做吗?
public class RandomPractise {
//1st find out a vowel
public Boolean isVowel(char c) {
boolean isV = true;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
isV = true;
} else {
isV = false;
}
return isV;
}
public void randomTest() {
String str = "boat";
char[] c1 = str.toCharArray();
int n = str.length();
for (int i = 0; i < n/2; i++) {
if (!(isVowel(c1[i])) && !(isVowel(c1[n - i - 1]))) {
char temp = c1[i];
c1[i] = c1[n - i - 1];
c1[n - i - 1] = temp;
}
}
System.out.println(String.valueOf(c1));
}
public static void main(String[] args) {
RandomPractise r = new RandomPractise();
r.randomTest();
}
我想交换字符串中除元音字母以外的字符。那里有很多代码,但是,这是我正在处理的代码,我发现它很容易理解,但这并没有产生预期的结果。
public class RandomPractise {
//1st find out a vowel
public Boolean isVowel(char c) {
boolean isV = true;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
isV = true;
} else {
isV = false;
}
return isV;
}
//Check current and next chars if not vowel do the swap
public void randomTest() {
String str = "boat";
char[] c1 = str.toCharArray();
for (int i = 0; i < c1.length - 1; i++) {
if (!(isVowel(c1[i])) && !(isVowel(c1[i + 1]))) {
char temp = c1[i];
c1[i] = c1[i + 1];
c1[i + 1] = temp;
}
}
System.out.println(String.valueOf(c1));
}
public static void main(String[] args) {
RandomPractise r = new RandomPractise();
r.randomTest();
}
示例:如果我使用输入:boat,我希望看到输出:toab [这不会发生]。当我使用输入时:sboath 我看到输出 bsoaht。
问题:我应该做些什么改变才能使其适用于船?
提前感谢您的宝贵时间。
"Question: What change should I do to make it work for boat?"
你是要这样做吗?
public class RandomPractise {
//1st find out a vowel
public Boolean isVowel(char c) {
boolean isV = true;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
isV = true;
} else {
isV = false;
}
return isV;
}
public void randomTest() {
String str = "boat";
char[] c1 = str.toCharArray();
int n = str.length();
for (int i = 0; i < n/2; i++) {
if (!(isVowel(c1[i])) && !(isVowel(c1[n - i - 1]))) {
char temp = c1[i];
c1[i] = c1[n - i - 1];
c1[n - i - 1] = temp;
}
}
System.out.println(String.valueOf(c1));
}
public static void main(String[] args) {
RandomPractise r = new RandomPractise();
r.randomTest();
}