请教一个问题 进来看看 在线等

lysoft 2004-03-29 01:07:50
有一段程序
import java.util.StringTokenizer;
class ShowToken{
public static void main(String[] args){
StringTokenizer str1;
String str="I Love This Game.";
str1=new StringTokenizer(str);
System.out.println("1:"+str1.nextToken());
System.out.println("2:"+str1.nextToken());
System.out.println("3:"+str1.nextToken());
System.out.println("4:"+str1.nextToken());
}
}
这段代码的字符串str是已经提前给好的,所以在后面的输入时候,直到要用几个print;能不能再不知道的情况下,自动来用循环实现。比如给了个字符串i iiii iiii i iii iii,应该有6个print。这样的情况怎样实现。
...全文
57 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lysoft 2004-03-29
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2898/2898759.xml?temp=.3480799
来这里 接分
lysoft 2004-03-29
  • 打赏
  • 举报
回复
==你来接分 softgq(英雄小强) 哈哈 我两个都用了 谢谢了 先给他 ==你来!
lysoft 2004-03-29
  • 打赏
  • 举报
回复
softgq(英雄小强)
你的我试了,但是有问题 什么也没与输出!呵呵
softgq 2004-03-29
  • 打赏
  • 举报
回复
import java.util.StringTokenizer;
class ShowToken{
public static void main(String[] args){
StringTokenizer str1;
String str="I Love This Game.";
str1=new StringTokenizer(str);
System.out.println(""+str1.countTokens());
int n=str1.countTokens();
for(int i=0;i<n;i++)
{
System.out.println(i+""+str1.nextToken());
}
}
}
这回没问题了,我试过了
mars_han 2004-03-29
  • 打赏
  • 举报
回复
用空格字符判断字符数量:spacenum+1 is printnum.
softgq 2004-03-29
  • 打赏
  • 举报
回复
补上一个错误
for(int i=0;i>=str1.countTokens();i++)
_____
goodsong 2004-03-29
  • 打赏
  • 举报
回复
import java.util.StringTokenizer;
class ShowToken{
public static void main(String[] args){
StringTokenizer str1;
String str="I Love This Game.";
str1=new StringTokenizer(str);
int i=0;
while(str1.hasMoreTokens())
{
i++;
System.out.println("i:"+str1.nextToken());
}
}
softgq 2004-03-29
  • 打赏
  • 举报
回复
int countTokens();
Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception.
也就是你这么做
for(int i=0;i<str1.countTokens();i++)
{
System.out.println(i+""str1.nextToken());
}
OK
等加分了哦

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧