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

问题描述 投票:0回答:2
  1. 用户输入一个字符串。
  2. 如果猫和狗出现相同的时间,程序返回true,否则为false。
  3. 例如:自catcatdoghotdogreturns TRUE以来cat = 2 = dog = 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:String运算符未定义+运算符。

任何建议,真的很受欢迎

java string
2个回答
2
投票

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

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

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

删除comma

© www.soinside.com 2019 - 2024. All rights reserved.