Java返回值取决于字符串中猫和狗的出现?

标签 java string

  1. 用户输入一个字符串。

  2. 如果猫和狗出现的时间相同,则程序返回 true,否则返回 false。

  3. 示例:catcatdoghotdog = 返回 TRUE,因为 cat = 2dog = 2

//我的代码

 public String three (String str) 
{
  int cat = 0;
  int dog = 0;

  for(int a = 0; a < str.length() - 2; a++)
  {
    if (str.substring(a, a+3).equals("cat"))
    {
      cat++;
    }

    if (str.substring(a, a+3).equals("dog"))
    {
      dog++;
    }
  }

  if(dog == cat)
  {
      return "TRUE";
  }

  else
  {
      return "FALSE";
  }
}

System.out.println("Number of times cat and dogs appear in your word: " , + response.three(word)); // doesn't work... 

ERROR: + operator is undefined for String argument.

任何建议,非常欢迎

最佳答案

要连接字符串和变量,使用+就足够了。删除,

System.out.println("Number of times cat and dogs appear in your word: " + response.three(word));

关于Java返回值取决于字符串中猫和狗的出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47916775/

相关文章:

java - 如何在 Spring 通过注释设置 Activity 配置文件?

java - 获取另一个类中的 SharedPreferences

java - 一起使用 String 和 int 时的 System.out.println 行为

java - 检查干草堆是否包含一组针的最快方法

javascript - 如何在 JavaScript 中将字符串转换为整数

计算长度小于n的字符串中的单词数

java - PDFBox:处理非常大的 PDF。

java - 使用 @XmlAnyElement 的 XML 映射

java - 在 Java 2D API 中将图像缩放为单个表面

C++ 字符串附加格式化数据