【发布时间】:2015-10-24 12:19:34
【问题描述】:
这是我的作业:
接受一个句子并打印连续字符相等的单词
输入:一天留一个苹果
输出:苹果保留
这是我的工作:
import java.util.*;
public class Program1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=sc.nextLine();
String str=s.toLowerCase();
int l,i=0; char c,d;int a,b,m=0;int n=0; String r=""; String res="";
l=s.length();
str=" "+str+" ";
for(i=0;i<(l-1);i++)
{
c=str.charAt(i);
d=str.charAt(i+1);
a=c;
b=d;
m=str.indexOf(' ');
n=str.indexOf(' ',(i+1));
if(d==' ')
{
m=str.indexOf(' ',(i-1));
n=str.indexOf(' ',(i+1));
}
if(a==b)
{
r=str.substring(m,n);
res=res +" "+ r;
}
}
System.out.println(res);
}
}
它被编译,但它没有给出正确的输出。
如果我输入上面的例子,它会返回:
an apple an apple a day keeps
我需要做什么?
【问题讨论】:
-
它不运行?您提供输入,程序打印输出。
标签: java string character bluej