我需要我的输出只有5个字符长,不计算删除的元音。目前我的代码是计算输入长度并返回该数字减去元音。这可能会令人困惑。如果我输入" idontthinkso"它只会返回" dnt"而不是我想要它打印出来的是" dntth"。顺便说一句,我不允许使用Stringbuilder或类似的东西,只有一个循环,所以请原谅代码。我怎样才能解决这个问题?这是我的代码:
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);
}
}
答案 0 :(得分:1)
你可以更简单地做到这一点。在这里,我迭代输入中的每个字符,如果达到限制则中断:
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 tweetLengthAllowed = 5;
int i = 0;
boolean isNotVowel;
boolean limitReached;
for (char c : input.toCharArray()) {
isNotVowel = "AEIOUaeiou".indexOf(c) == -1;
limitReached = tweetLengthAllowed <= i;
if (limitReached) { // exit the loop
break;
} else if (isNotVowel) { // append the char
s += c;
i++;
}
}
System.out.println(s);
}
}
运行输出:
输入推文:
idontthinkso
dntth
答案 1 :(得分:1)
我非常非常赞成使用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("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
另外,如果你要使用一大堆OR,请帮我个忙,让它更具可读性。
答案 2 :(得分:1)
这是我的做法:-
import java.util.Scanner;
import java.lang.StringBuffer;
public class Program {
private static String RemoveVowel(String text)
{
int len = text.length();
char[]vowels = {'a','e','i','o','u','A','E','I','O','U'};
StringBuffer sb = new StringBuffer(text);
for(int i = 0;i<len;i++)
{
for(char v : vowels)
{
if(v == text.charAt(i))
{
sb.setCharAt(i,'\0');
}
}
}
return sb.toString();
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter some text to remove vowels from it: ");
String val = scan.nextLine();
System.out.println(RemoveVowel(val));
}
}
我使用字符串缓冲区使字符串可修改,并且“for”循环遍历字符串的长度,并遍历“元音”数组; “if”语句检查当前字符是否等于元音之一,如果为真,则将当前字符设置为空,从而删除元音。
答案 3 :(得分:0)
public class RemoveVowels {
public static void main(String[] args) {
String inputString = "Java - Object Oriented Programming Language";
System.out.println(inputString.replaceAll("[aeiouAEIOU]", " "));
}
}
输出:
J v - bj ct r nt d Pr gr mm ng L ng g