如何在Java中跳过一个块?

sun*_*sun 3 java loops for-loop breakpoints

在给出的程序中,我必须确保如果两个连续的字符是相同的.我不应该增加变量的值(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);
    }
}
Run Code Online (Sandbox Code Playgroud)

ben*_*ico 6

您可以使用该关键字continue来完成您要执行的操作.

但是,您也可以反转条件测试,并且count++仅在它不同时使用(!=而不是==在if中),否则不执行任何操作