[英]Are there any ways to make this simple program more efficient?
The program is pretty simple: it takes a string and replaces the vowels with '_' then prints the reversed string.该程序非常简单:它接受一个字符串并用“_”替换元音,然后打印反转的字符串。 I am just looking at ways to make my code more professional and would like some tips.
我只是在寻找使我的代码更专业的方法,并希望获得一些提示。
import java.util.Scanner;
public class reverse_string {
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
String input, reverseInput;
String legolas = new String();
System.out.println("Enter any input");
input = scan.nextLine();
StringBuilder newString = new StringBuilder(input);
System.out.println("The data you entered is: "+input);
for (int i =0; i <input.length();i++){
if (testIfVowel(input.charAt(i))){
newString.setCharAt(i,'_');
}
else{
newString.setCharAt(i, input.charAt(i));
}
}
for(int i = input.length()-1;i>=0;i--){
legolas = legolas +input.charAt(i);
}
reverseInput=reverseOrder(newString);
System.out.println("Your old data was: "+input+"\nYour new data is: "+newString +"\nYour old data in reverse is: "+legolas+"\nYour new data in reverse is: "+ reverseInput);
}
public static boolean testIfVowel(char x){
if(x =='a'||x=='A'||x=='e'||x=='E'||x=='i'||x=='I'||x=='o'||x=='O'||x=='u'||x=='U'){
return true;
}
else{
return false;
}
}
public static String reverseOrder(StringBuilder x){
String string= new String();
for(int i = x.length()-1;i>=0;i--){
string = string + x.charAt(i);
}
return string;
}
}
Since you are only replacing vowels with _
why not use the replaceAll
method java provided for you with a bit of regex that matches vowels.既然您只是用
_
替换元音,为什么不使用 java 为您提供的replaceAll
方法和一些与元音匹配的正则表达式。
str.replaceAll("[aeiou]", "_");
and reverse it using并使用反转它
reverseInput = new StringBuilder(str).reverse().toString()
So this is how your final code should look like after the improvements所以这就是你的最终代码在改进后的样子
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String input, reverseInput;
System.out.println("Enter any input");
input = scan.nextLine();
reverseInput = new StringBuilder(input).reverse().toString();
String withoutVowels = input.replaceAll("[aeiou]", "_");
String withoutVowelsReversed = new StringBuilder(withoutVowels).reverse().toString();
System.out.println("The data you entered is: " + input);
System.out.println("Your old data was: " + input + "\nYour new data is: " + withoutVowels + "\nYour old data in reverse is: " + reverseInput + "\nYour new data in reverse is: " + withoutVowelsReversed);
}
I would suggest that your logic is unnecessarily complex.我建议您的逻辑不必要地复杂。 For instance, do you really need a
testIfVowel
method that is only used one time?例如,你真的需要一个只使用一次的
testIfVowel
方法吗? I doubt it.我对此表示怀疑。
I'd do something like this (pseudocode):我会做这样的事情(伪代码):
resultString = '';
for (i = 0; i < length(s); i++) {
if ("AEIOUaeiou".indexOf(s[i]) != 0) {
resultString = '_' + resultString;
} else {
resultString = s[i] + resultString;
}
}
(You could do this in any language.) (您可以使用任何语言执行此操作。)
Notice how the test-for-vowels is now simple and obvious, clearly working for both upper- and lower-case versions.请注意元音测试现在如何简单明了,显然适用于大写和小写版本。 And, how the "reversed string" is created, one character at a time, simply by inserting the new character at the front of the initially-empty resultString.
并且,如何创建“反向字符串”,一次一个字符,只需在初始为空的 resultString前面插入新字符即可。
Perhaps most importantly (to me), this version "reads very easily."也许最重要的是(对我来说),这个版本“读起来很容易”。 I don't have to look at much code to desk-check that it will work.
我不需要查看太多代码来检查它是否可以工作。 It, if you will, "gets straight to the point."
如果你愿意,它“直截了当”。
Much cleaner, I think ...干净多了,我觉得……
This doesn't seem like it needs to be harder than:这似乎不需要比以下更难:
String result =
new StringBuilder(input.replaceAll("[AEIOUaeiou]", "_"))
.reverse()
.toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.