如何在Java中跳过块?

如何在Java中跳过块?,java,loops,for-loop,breakpoints,Java,Loops,For Loop,Breakpoints,在给定的程序中,我必须确保两个consequentive字符是否相同。我不应该增加变量(Count)的值。。。我曾尝试过“break;”,但这使我跳出了“for循环”,这是非常适得其反的。如何跳过给定的部分,仍然继续“for循环” 目前,我对“Hello//world”的输出是3。它应该是2(“/”表示一个“”(空格)) 代码 import java.util.Scanner; 类CountWordsWithEmergency { 公共静态void main() { 扫描仪输入=新扫描仪(Sys

在给定的程序中,我必须确保两个consequentive字符是否相同。我不应该增加变量(Count)的值。。。我曾尝试过“break;”,但这使我跳出了“for循环”,这是非常适得其反的。如何跳过给定的部分,仍然继续“for循环”

目前,我对“Hello//world”的输出是3。它应该是2(“/”表示一个“”(空格))

代码
import java.util.Scanner;
类CountWordsWithEmergency
{
公共静态void main()
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入字符串”);
字符串inp=input.nextLine();
System.out.println(“谢谢”);
int i=inp.length();
整数计数=1;

对于(int j=0;j您要查找的单词是“”。

在要跳过块的位置尝试使用continue。

您可以使用关键字
continue
,以完成您尝试执行的操作


但是,您也可以反转您的条件测试,仅当它不同时才使用
count++
!=
,而不是if中的
=
),并且当您想要中断当前迭代时,不做任何其他事情

if ((inp.charAt(j+1)) != check)  {
    count++;
}
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);
    }
}
import java.util.Scanner;
类CountWordsWithEmergency
{
公共静态void main()
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入字符串”);
字符串inp=input.nextLine();
System.out.println(“谢谢”);
int i=inp.length();
整数计数=1;
对于(int j=0;j试试这个:

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

通过检查
!=

增加计数的值。您可能需要使用
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);
  }
}

希望有帮助:)

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


continue语句仅在while、do while和for循环中使用。

好的,所以您的解决方案是这样的。.解释得很好。没有其他人解释您的答案是好的。
!=
是一种比较,而不是增量。
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);
  }
}
int wordsCount = str.split(' ').length;