本文介绍了While循环未运行indexOf搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一个字符串在另一个字符串中出现了多少次.对于我的测试,我对单词One使用"ea",对于单词Two使用"Ilikedthebestontheeastbeachleast".我的输出为外观"变量返回0,该变量应存储"ea"在wordTwo中出现的次数.

I'm trying to find out how many times one string appears in another. For my testing, I'm using "ea" for wordOne and "Ilikedthebestontheeastbeachleast" for wordTwo. My output is returning 0 for my "appearance" variable, which should store how many times "ea" appears in wordTwo.

这是相关的代码部分:

int wordTwoLength = wordTwo.length();
  System.out.println(wordTwoLength);

  while (wordTwoLength > 0)
  {
     positionCount = wordTwo.indexOf(wordOne, positionCount);
     appearances = appearances++;
     wordTwoLength = (wordTwoLength - positionCount);
  }
  System.out.println(appearances);

推荐答案

 appearances = appearances++;

这将确保外观始终为零.

This will make sure appearances is ZERO always.

那不应该只是外表++吗?

Shouldn't it be just appearances++ ?

这篇关于While循环未运行indexOf搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:22