gpt4 book ai didi

java - 为什么返回空白?

转载 作者:行者123 更新时间:2023-12-01 06:32:29 25 4
gpt4 key购买 nike

这来 self 们老师给我们的单词库,我应该返回最长的单词,其中仅包含键盘顶行的字符。目前它返回空白。请帮忙。

//What's the longest word only using the top row of the keyboard?      
public static void Question6() {

String longestWordSoFar = " ";
System.out.println("Question 6:");
for(int i = 1; i < WordList.numWords(); i++) // check every word in wordlist
{
if(topRow(WordList.word(i))) { // if the length is greater than the previous word, replace it
{
if(WordList.word(i).length() > longestWordSoFar.length())
longestWordSoFar=WordList.word(i);
}
}

}
System.out.println("longest word including top row: " + longestWordSoFar);
System.out.println();
return;
}

public static boolean topRow(String word) {

for(int i = 0; i < word.length(); i++) {
//return true if the word has all of the letters in the top row of the keyboard
if (word.charAt(i) != 'q') {
return false;
}
if (word.charAt(i) != 'w') {
return false;
}
if (word.charAt(i) != 'e') {
return false;
}
if (word.charAt(i) != 'r') {
return false;
}
if (word.charAt(i) != 't') {
return false;
}
if (word.charAt(i) != 'y') {
return false;
}
if (word.charAt(i) != 'u') {
return false;
}
if (word.charAt(i) != 'i') {
return false;
}
if (word.charAt(i) != 'o') {
return false;
}
if (word.charAt(i) != 'p') {
return false;
}
}
return true;
}

最佳答案

改用正则表达式。如果 sString 类型,则使用

s.matches("[qwertyuiop]+")

匹配键盘顶部一行上的一个或多个字母。我将不区分大小写留给您。

附:我打赌答案是“打字机”。

关于java - 为什么返回空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19536333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com