3

与えられたプログラムでは、2つの連続する文字が同じであるかどうかを確認する必要があります。変数(Count)の値を増やすべきではありません...「break;」を試しましたが、非常に逆効果である「forループ」からスキ​​ップされます。指定された部分をスキップして「forループ」を続行するにはどうすればよいですか?

現在、「Hello // world」の出力は3です。2である必要があります(「/」は「」(スペース)を示します)。

コード

import java.util.Scanner;
class CountWordsWithEmergency
{
    public static void main()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input the String");
        String inp = input.nextLine();
        System.out.println("thank you");
        int i = inp.length();
        int count = 1;
        for(int j=0;j<=i-1;j++) //This is the for loop I would like to stay in.
        {
            char check = inp.charAt(j);
            if(check==' ')
            {
                if((inp.charAt(j+1))==check) //This is the condition to prevent increase for
                                             //count variable.
                {
                    count = count; //This does not work and neither does break;
                }
                count++;
            }
        }
        System.out.println("The number of words are : "+count);
    }
}
4

9 に答える 9

6

continueあなたがやろうとしていることを達成するためにキーワードを使うことができます。

ただし、条件付きテストを逆にしてcount++、それが異なる場合にのみ使用し(if!=ではなく==)、それ以外の場合は何もしないこともできます。

于 2013-03-09T13:47:32.883 に答える
3
if ((inp.charAt(j+1)) != check)  {
    count++;
}
于 2013-03-09T13:48:51.400 に答える
2

あなたが探している言葉は「続ける」です。

于 2013-03-09T13:47:27.830 に答える
2

これを試して:

if ((inp.charAt(j+1)) != check)  {
    count++;
}

でチェックして、カウントの値をインクリメントします!=

于 2013-03-09T13:49:35.997 に答える
1

ブロックをスキップする場所で続行を使用してみてください。

于 2013-03-09T13:47:28.757 に答える
1

続行;」を使用します 現在の反復を中断したいとき。

于 2013-03-09T13:48:15.740 に答える
1

continueは、コードのループまたはブロックをスキップし、新しい条件でループを再実行するために使用されるJavaプログラミングのキーワードです。

continueステートメントは、while、do while、およびforループでのみ使用されます。

于 2014-04-30T16:05:07.340 に答える
0

以下が機能するはずです。

import java.util.Scanner;
class CountWordsWithEmergency
{
    public static void main()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input the String");
        String inp = input.nextLine();
        System.out.println("thank you");
        int i = inp.length();
        int count = 1;
        for(int j=0;j<=i-1;j++) //This is the for loop I would like to stay in.
        {
            char check = inp.charAt(j);
            if(check==' ')
            {
                if((inp.charAt(j+1))==check) //This is the condition to prevent increase for
                                             //count variable.
                {
                    continue;
                }
                count++;
            }
        }
        System.out.println("The number of words are : "+count);
    }
}
于 2013-03-09T13:49:27.673 に答える
0

キーワードを使用するcontinueか、ロジックを少し変更することをお勧めします。

import java.util.Scanner;
class CountWordsWithEmergency  
{
  public static void main()
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Please input the String");
    String inp = input.nextLine();
    System.out.println("thank you");
    int i = inp.length();
    int count = 1;
    for(int j=0;j<=i-1;j++) //This is the for loop I would like to stay in.
    {
      char check = inp.charAt(j);
      if(check==' ')
      {
        if((inp.charAt(j+1))!=check)
        {
          count++;
        }
      }
    }
    System.out.println("The number of words are : "+count);
  }
}

編集:

クラスのsplitメソッドを使用することをお勧めします。String

int wordsCount = str.split(' ').length;

それが役に立てば幸い :)

于 2013-03-09T13:53:53.270 に答える