如何在Java中跳过一个块?

时间:2013-03-09 13:45:51

标签: 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);
    }
}

9 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:3)

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

答案 2 :(得分:2)

您要找的字是“continue”。

答案 3 :(得分:2)

试试这个:

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

通过!=确认增加点数值。

答案 4 :(得分:1)

尝试在想要跳过块的地方继续使用。

答案 5 :(得分:1)

使用“continue;”当你想打破当前的迭代时。

答案 6 :(得分:1)

continue是java编程中的一个关键字,用于跳过循环或代码块并使用新条件重新执行循环。

continue语句仅用于while,do while和for循环。

答案 7 :(得分: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);
    }
}

答案 8 :(得分: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;

希望有所帮助:)