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)
| 归档时间: |
|
| 查看次数: |
1284 次 |
| 最近记录: |