使用循环从输入字符串中删除元音 [英] Removing vowels from an input string using a loop
问题描述
我需要我的输出只有5个字符长不算去除元音。目前我的代码是计数输入长度,并返回该数字减去元音。这可能会让人困惑。如果我输入idontthinkso它只返回dnt,而不是我想要打印出来的是dntth。顺便说一句,我不能使用Stringbuilder或类似的东西,只有一个循环,所以原谅的代码。我怎样才能解决这个问题?这是我的代码:
pre $ import $ java.util.Scanner;
public class TweetCompressor {
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
String s =;
System.out.println(输入推文:);
String input = keyboard.nextLine();
int f = 0;
int tweetLengthAllowed = 5;
(int i = 0; i< tweetLengthAllowed; i ++){
char c = input.charAt(i);
if(c =='a'||
c =='e'||
c =='i'||
c =='o'||
c =='u'||
c =='A'||
c =='E'||
c =='I'||
c =='O' ||
c =='U'){
f = 1;
} else {
s = s + = c;
f = 0;
}
}
System.out.println(s);
$ div $解析方案
'm 非常赞成为此使用while循环,但是由于您声明只能使用for循环...
问题是你的循环会迭代到i = 5,即使检测到元音。我们需要一种方式来告诉循环假装从未发生。你不能减少我,否则你将永远卡在同一个字符。
这是我想出来的,我决定只增加 tweetLengthAllowed
来取消我的增量。
import java.util.Scanner;
public class Main {
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
String s =;
System.out.println(输入推文:);
String input = keyboard.nextLine();
int f = 0;
int tweetLengthAllowed = 5;
for(int i = 0; i< tweetLengthAllowed; ++ i){//必须是for循环
char c = input.charAt(i);
if(c =='a'|| c =='e'|| c =='i'|| c =='o'|| c =='u'||
c =='A'|| c =='E'|| c =='I'|| c =='O'|| c =='U'){
f = 1;
tweetLengthAllowed ++; //允许循环继续进行一次以上的交互
} //如果
else else $ {
s = s + = c;
f = 0;
} // end else
} // end for
System.out.println(s);
$ b $ // end main
// end class
另外,如果你打算使用一个大型的ORs链,请为你自己一个忙,让它更具可读性,如上所述。
I need my output to only be 5 characters long NOT counting the removed vowels. Currently my code is counting the input length and returning that number minus the vowels. This might be confusing. If I input "idontthinkso" it only returns "dnt" instead of what I want it to print out which is "dntth". Btw, I'm not allowed to use Stringbuilder or anything like that, only a loop, so forgive the code. How can I fix this? Here is my code:
import java.util.Scanner;
public class TweetCompressor {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String s = "";
System.out.println("Type a tweet: ");
String input = keyboard.nextLine();
int f = 0;
int tweetLengthAllowed = 5;
for (int i = 0; i < tweetLengthAllowed; i++) {
char c = input.charAt(i);
if (c == 'a' ||
c == 'e' ||
c == 'i' ||
c == 'o' ||
c == 'u' ||
c == 'A' ||
c == 'E' ||
c == 'I' ||
c == 'O' ||
c == 'U') {
f = 1;
} else {
s = s += c;
f = 0;
}
}
System.out.println(s);
}
}
I'm very much in favor of using a while loop for this, but since you stated you can only use a for loop...
The problem is that your loop will iterate until i = 5, even if a vowel is detected. We need a way to tell the loop to pretend that never happened. You can't decrement i, or you'll be stuck at the same character forever.
Here's what I came up with, I decided to simply increment the tweetLengthAllowed
to negate the i increment.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String s = "";
System.out.println("Type a tweet: ");
String input = keyboard.nextLine();
int f = 0;
int tweetLengthAllowed = 5;
for(int i = 0; i < tweetLengthAllowed; ++i) { //Must be a for loop
char c = input.charAt(i);
if(c == 'a'|| c == 'e'|| c == 'i'|| c == 'o'|| c =='u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
f = 1;
tweetLengthAllowed++; //Allows the loop to continue for one more interation
} //end if
else{
s = s += c;
f = 0;
}//end else
} //end for
System.out.println(s);
} //end main
} //end class
Also, if you're going to use a big chain of ORs, please do yourself a favor and make it more readable as I did above.
这篇关于使用循环从输入字符串中删除元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!