与えられたプログラムでは、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);
}
}