level 12
在自学到第五章字符串时,就startsWith方法和endsWith方法书上给的一例进行了实际的操作,编写完后运行出现了错误;不知为何?请各位能否帮忙解答下!谢谢!
2012年07月25日 20点07分
1
level 12
源码是这样的:
class StartEndTest
{
public static void main(String[] args)
{
String[] companyArray={"沈阳天天有限责任公司",
"北京龙腾有限公司",
"沈阳五星集团",
"北京长远集团"
};
//输出在北京的企业
System.out.println("在北京的企业:");
for(int i=0;i<companyArray.length;i++)
{
if(companyArray[i].startsWith("北京"))
{
System.out.println(companyArray[i]);
}
}
System.out.println();
//输出所有的集团
System.out.println("集团性质企业:");
for(int i=0;i<companyArray.length;i++)
{
if(companyArray[i].endsWith("集团"))
{
System.out.println(companyArray[i]);
}
}
}
}
2012年07月25日 21点07分
2
level 12
出现错误后我把后半部删除了只保留了startsWith部分:
class StartEndTest
{
public static void main(String[] args)
{
String[] companyArray={"沈阳天天有限责任公司",
"北京龙腾有限公司",
"沈阳五星集团",
"北京长远集团"
};
//输出在北京的企业
System.out.println("在北京的企业:");
for(int i=0;i<companyArray.length;i++)
{
if(companyArray[i].startsWith("北京"))
{
System.out.println(companyArray[i]);
}
}
}
}
运行后正常,结果如下:
在北京的企业:
北京龙腾有限责任有限公司
北京长远集团
2012年07月25日 21点07分
3
level 12
上面运行结果有一处打错了:
应该是:
在北京的企业:、
北京龙腾有限责任公司
北京长远集团
2012年07月25日 21点07分
4
level 16
你 2 楼给出的代码,和你图片上有错的代码明显有不一样的地方
完全按 2 楼的代码,是可以的
你图片的上少了一个大括号
2012年07月25日 22点07分
10