2

したがって、私が持っているコードは、ユーザーが文(文字列)を入力し、文字列を検索して最小の単語を返す必要がある宿題用です。ただし、文字列の最初の場所に数字を入力する必要があります。例:「4これは何ですか」。出力は「IS」であり、数値を無視する必要があります。番号を無視する方法を理解する唯一の方法は、番号がある最初のスポットをループでスキップすることです。それはそれ自体で動作しますが、プログラムの残りの部分に入れると動作を停止します。このプログラムをよりクリーンにする方法はありますか?

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]);
}
4

4 に答える 4

1

forループ ( で開始する必要があります) 内i = 0に、次のようなコードを追加します。

try {
  double value = Double.parseDouble(myWords[i]);
} catch (NumberFormatException e) {
  // add the rest of your code here
}

アイデアは、単語を数値に変換しようとすることです。失敗した場合、それは数値ではないことを意味するため、単語に対して長さロジックを使用できます。

于 2012-09-09T19:51:10.190 に答える
0

以下は基本的にコードを短くするだけです..それ以外はあまり変わりません. そうは言っても、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]);
}

推奨コード:

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];
}
于 2012-09-15T06:27:10.230 に答える
0

最初にすべきことは、演習に関連するコードと、入力ストリームから行を読み取るようなものを混在させるのではなく、使用する関数を作成することです。

を使用して、文字が文字かどうかをテストできますCharacter.isLetter(char)。適切な演習は、その関数のみを使用してソリューションを構築し、ループ内で各文字 (String.charAt(int)メソッド) を個別に調べることです。解決策は、現在最も短い単語の開始位置とその長さを覚えておくことです。


実際には、次のような正規表現を使用します。

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;
}
于 2012-09-09T20:08:57.320 に答える
0

たとえば、部分文字列を使用してみることができます

String result=inputString.substring(1)

'1' は文字列の 2 番目の文字であり、最初の値を保存してすべての値を返す部分文字列です。

于 2012-09-09T20:14:34.377 に答える