在给定的程序中,我必须确保两个连续字符是否相同。我不应该增加变量 (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);
}
}