Java 优化代码。搜索字符串
因此,我的代码用于家庭作业,用户输入一个句子(字符串),我需要搜索字符串并返回最小的单词。但是,必须在字符串的第一个位置输入一个数字。例:“这是什么?”。输出应为“是”,忽略数字。我知道如何忽略数字的唯一方法是让循环跳过数字所在的第一个点。它自己工作,但每当我把它放进程序的其余部分,它就停止工作。有没有办法让这个程序更干净Java 优化代码。搜索字符串,java,string,Java,String,因此,我的代码用于家庭作业,用户输入一个句子(字符串),我需要搜索字符串并返回最小的单词。但是,必须在字符串的第一个位置输入一个数字。例:“这是什么?”。输出应为“是”,忽略数字。我知道如何忽略数字的唯一方法是让循环跳过数字所在的第一个点。它自己工作,但每当我把它放进程序的其余部分,它就停止工作。有没有办法让这个程序更干净 public static void main(String[] args) { Scanner sc = new Scanner(System.in); /
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Lexicographically smallest word
String TheSentence = sc.nextLine();
String[] myWords = TheSentence.split(" ");
int shortestLengths, shortestLocation;
shortestLengths = (myWords[1]).length();
shortestLocation = 1;
for (int i = 1; i < myWords.length; i++) {
if ((myWords[i]).length() < shortestLengths) {
shortestLengths = (myWords[i]).length();
shortestLocation = i;
}
}
System.out.println(myWords[shortestLocation]);
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
//词典学上最小的词
字符串thecontence=sc.nextLine();
String[]myWords=thecontent.split(“”);
int最短长度,最短位置;
最短长度=(myWords[1]).length();
最短位置=1;
for(int i=1;i
在for
循环(应该从i=0开始)的中,添加如下代码:
try {
double value = Double.parseDouble(myWords[i]);
} catch (NumberFormatException e) {
// add the rest of your code here
}
public static String shortestWord(String sentence) {
String shortest = null;
Pattern word = Pattern.compile("\\w+");
Matcher m = word.matcher(sentence);
while (m.find()) {
String candidate = m.group();
if (shortest == null || shortest.length() > candidate.length())
shortest = candidate;
}
return shortest;
}
这个想法是,你尝试将你的单词转换成一个数字,如果你失败了,这意味着它不是一个数字,因此你可以在单词上使用长度逻辑。你应该做的第一件事是创建你想要使用的函数,而不是将练习的相关代码与从输入流中读取一行这样的事情混合在一起
您可以使用character.isleter(char)
测试字符是否为字母。
一个好的练习是只使用该函数构建一个解决方案,并分别查看循环中的每个字符(String.charAt(int)
method)。
解决方法是记住当前最短单词的起始位置和长度
实际上,我只会使用这样的正则表达式:
try {
double value = Double.parseDouble(myWords[i]);
} catch (NumberFormatException e) {
// add the rest of your code here
}
public static String shortestWord(String sentence) {
String shortest = null;
Pattern word = Pattern.compile("\\w+");
Matcher m = word.matcher(sentence);
while (m.find()) {
String candidate = m.group();
if (shortest == null || shortest.length() > candidate.length())
shortest = candidate;
}
return shortest;
}
您可以尝试使用子字符串,例如
String result=inputString.substring(1)
“1”是字符串中的第二个字母,子字符串返回除第一个字母外的所有值。下面的内容基本上只是缩短了代码。除此之外,它没有太大变化。也就是说,最好用一个名为shortestWord()的方法或其他方法来创建所有这些。但是,下面的代码没有理由不起作用
修订守则:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] myWords = (sc.nextLine()).split(" ");
int shortestLocation = 1
for (int i = 2; i < myWords.length; i++) { // No reason to start at 1 as you have
// already made shortestLocation = 1
if (myWords[i].length() < myWords[shortestLocation].length()) {
shortestLocation = i;
}
}
System.out.println(myWords[shortestLocation]);
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串[]myWords=(sc.nextLine()).split(“”);
int shortestLocation=1
对于(inti=2;i
建议代码:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] myWords = (sc.nextLine()).split(" ");
System.out.println("The shortest word is: " + shortestWord(myWords));
}
public static String shortestWord(String[] myWords) {
int shortestLocation = 1
for (int i = 2; i < myWords.length; i++) { // No reason to start at 1 as you have
// already made shortestLocation = 1
if (myWords[i].length() < myWords[shortestLocation].length()) {
shortestLocation = i;
}
}
return myWords[shortestLocation];
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串[]myWords=(sc.nextLine()).split(“”);
System.out.println(“最短的单词是:”+最短的单词(myWords));
}
公共静态字符串最短字(字符串[]myWords){
int shortestLocation=1
对于(inti=2;i
像“123a abc ab3”这样的句子怎么样?是最短的单词“a”、“abc”、“ab”或“ab3”?与答案一起,不要忘记将shortestLocation=0代码>在您的代码中。