gpt4 book ai didi

java - 如何使用嵌套 for 循环计算文件或字符串对象中的单词数?

转载 作者:行者123 更新时间:2023-12-01 04:13:58 24 4
gpt4 key购买 nike

这是一个项目,大部分已经完成,但我需要能够计算字符串或文件中的单词数。我必须使用嵌套的 for 循环,并且必须使用包含单词分隔符的字符串。现在这就是我所拥有的:

public static int wordCounter(String text)
{
String WORDS_GROUP = ",\n ";
String text= "This is my sample text";
int wordCount=0;
for(int i=0; i<text.length(); i++){
for(int j=0; j<WORDS_GROUP.length(); j++){
if(text.charAt(i)==WORDS_GROUP.charAt(j)){
wordCount++;
}
}
}
}

最佳答案

您可以维护一个代表您的“状态”的 boolean 值。您是在一个空格 block 中(true),还是在一个非空格 block 中(false)?

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone {
public static void main(String[] args) throws java.lang.Exception {
String WORDS_GROUP = ",\n ";
String text = "This is my sample text";
int wordCount = 0;
boolean previousCharWasSpace = true;
for (int i = 0; i < text.length(); i++) {
boolean thisCharIsASpace = false;
for (int j = 0; j < WORDS_GROUP.length(); j++) {
if (text.charAt(i) == WORDS_GROUP.charAt(j)) {
previousCharWasSpace = true;
thisCharIsASpace = true;
break;
}
System.out.println("char=" + text.charAt(i) + " j=" + j
+ " previousCharWasSpace=" + previousCharWasSpace);
}
if (!thisCharIsASpace && previousCharWasSpace) {
wordCount++;
previousCharWasSpace = false;
System.out.println("char=" + text.charAt(i)
+ " previousCharWasSpace=" + previousCharWasSpace
+ " wordCount=" + wordCount);
}
}
System.out.println("wordCount=" + wordCount);
}
}

输出:

char=T j=0 previousCharWasSpace=true
char=T j=1 previousCharWasSpace=true
char=T j=2 previousCharWasSpace=true
char=T previousCharWasSpace=false wordCount=1
char=h j=0 previousCharWasSpace=false
char=h j=1 previousCharWasSpace=false
char=h j=2 previousCharWasSpace=false
char=i j=0 previousCharWasSpace=false
char=i j=1 previousCharWasSpace=false
char=i j=2 previousCharWasSpace=false
char=s j=0 previousCharWasSpace=false
char=s j=1 previousCharWasSpace=false
char=s j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char=i j=0 previousCharWasSpace=true
char=i j=1 previousCharWasSpace=true
char=i j=2 previousCharWasSpace=true
char=i previousCharWasSpace=false wordCount=2
char=s j=0 previousCharWasSpace=false
char=s j=1 previousCharWasSpace=false
char=s j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char=m j=0 previousCharWasSpace=true
char=m j=1 previousCharWasSpace=true
char=m j=2 previousCharWasSpace=true
char=m previousCharWasSpace=false wordCount=3
char=y j=0 previousCharWasSpace=false
char=y j=1 previousCharWasSpace=false
char=y j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char=s j=0 previousCharWasSpace=true
char=s j=1 previousCharWasSpace=true
char=s j=2 previousCharWasSpace=true
char=s previousCharWasSpace=false wordCount=4
char=a j=0 previousCharWasSpace=false
char=a j=1 previousCharWasSpace=false
char=a j=2 previousCharWasSpace=false
char=m j=0 previousCharWasSpace=false
char=m j=1 previousCharWasSpace=false
char=m j=2 previousCharWasSpace=false
char=p j=0 previousCharWasSpace=false
char=p j=1 previousCharWasSpace=false
char=p j=2 previousCharWasSpace=false
char=l j=0 previousCharWasSpace=false
char=l j=1 previousCharWasSpace=false
char=l j=2 previousCharWasSpace=false
char=e j=0 previousCharWasSpace=false
char=e j=1 previousCharWasSpace=false
char=e j=2 previousCharWasSpace=false
char= j=0 previousCharWasSpace=false
char= j=1 previousCharWasSpace=false
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char= j=0 previousCharWasSpace=true
char= j=1 previousCharWasSpace=true
char=t j=0 previousCharWasSpace=true
char=t j=1 previousCharWasSpace=true
char=t j=2 previousCharWasSpace=true
char=t previousCharWasSpace=false wordCount=5
char=e j=0 previousCharWasSpace=false
char=e j=1 previousCharWasSpace=false
char=e j=2 previousCharWasSpace=false
char=x j=0 previousCharWasSpace=false
char=x j=1 previousCharWasSpace=false
char=x j=2 previousCharWasSpace=false
char=t j=0 previousCharWasSpace=false
char=t j=1 previousCharWasSpace=false
char=t j=2 previousCharWasSpace=false
wordCount=5

你可以在这里玩这个:http://ideone.com/ASEjwB

关于java - 如何使用嵌套 for 循环计算文件或字符串对象中的单词数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19623484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com