どうすれば人はJavaで「イム」でその終わりを入力したエントリを数えることができますか?

アルティンMullaidrizi:

私はコンソールで書かれたエントリ(文字列)をカウントすること、「イム」で終わり、私はスターターとしてそれを行うことができますか?

import java.lang.String; 
import java.util.Scanner;

public class WordCount {
    public static void main (String[]args){

        final String SENTINEL = "END";
        Scanner sc = new Scanner(System.in);
        String text = "xy";

        do {
            System.out.print("Type a text or type "+SENTINEL+" when you are done. ");
            text = sc.nextLine();
            } while(!text.equalsIgnoreCase(SENTINEL));

            boolean IMcheck = text.endsWith("im");
            int count = 0;
            if(IMcheck == true){
                count++;
            }
            System.out.println("You have typed "+ count +" texts that end in \"im\" ");
        } 
    }
LuminousNutria:

コメンターが述べてきたように、あなたがdo-whileループ内であなたのif文を移動する必要があります。変数「IMcheck」を持つことは、ここでは不要です。

この問題を解決するために、私はあなたがあなたのif文の内側に「IMcheck」変数を割り当てるために使用されるコードを入れている、と私はあなたがdo-whileループにそれを移動しました。

public class WordCount {

   public static void main (String[]args) {
      final String SENTINEL = "END";
      int count = 0;
      Scanner sc = new Scanner(System.in);
      String text = "xy";

   do {
         if(text.endsWith("im"))
            count++;

         System.out.print("Type a text or type "+SENTINEL+" when you are done. ");
         text = sc.nextLine();

   } while(!text.equalsIgnoreCase(SENTINEL));

   System.out.println("You have typed "+ count +" texts that end in \"im\" ");
   }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=215457&siteId=1