Java 检查是否有某些字母以[cages]为边界并替换它们的算法?

Java 检查是否有某些字母以[cages]为边界并替换它们的算法?,java,string,algorithm,replace,Java,String,Algorithm,Replace,您好,我正在做以下编程练习:。声明如下: Story Old MacDingle had a farm. To be more precise, he had a free-range chicken farm. But Old MacDingle also had a fox problem. Foxes F eat chickens C At night the only guaranteed "safe" chickens are in their cages [] (unles

您好,我正在做以下编程练习:。声明如下:

Story

Old MacDingle had a farm.

To be more precise, he had a free-range chicken farm.

But Old MacDingle also had a fox problem.

Foxes F eat chickens C

At night the only guaranteed "safe" chickens are in their cages [] (unless a fox has got into the cage with them!)
Kata Task

Given the initial configuration of foxes and chickens what will the farm look like the next morning after the hungry foxes have been feasting?
Examples
Ex1 Before  

CCC[CCC]FCC[CCCCC]CFFFF[CCC]FFFF

After   

...[CCC]F..[CCCCC].FFFF[CCC]FFFF

Ex2 Before  

...[CCC]...[CCCFC].....[CCC]....

After   

...[CCC]...[...F.].....[CCC]....

Ex3 Before  

CCC[CCC]FCC[CCCFC]CFFFF[CCC]FFFF

After   

...[CCC]F..[...F.].FFFF[CCC]FFFF

Notes

    Anything not a fox, a chicken, or a cage is just dirt .
    All cages are intact (not open-ended), and there are no cages inside other cages
我编写了以下代码:

public class Dinglemouse {
  public static String hungryFoxes /*
At night the only guaranteed "safe" chickens are in their cages [] (unless a fox has got into the cage with them!)

As far as I understand if there are no cages then all chicken die anyway, fox or not, so

//If there are not cages, and there are foxes, all chickens are cleared; otherwise we keep the farm as it is.
if(!farm.contains("[") && !farm.contains("]")){
  if(farm.contains("F")){
     return farm.replace("C",".");
  }else{
    return farm;
  }
}
public-classdinglemouse{
公共静态字符串饥饿盒/*
晚上,唯一保证“安全”的鸡都在笼子里[](除非有狐狸和它们一起进了笼子!)

据我所知,如果没有笼子,那么所有的鸡都会死,不管是不是狐狸,也是如此

//If there are not cages all chickens are cleared
if(!farm.contains("[") && !farm.contains("]")){
  return farm.replace("C",".");
}
应替换为

       if(betweenCages.contains("F")){
代码的另一个问题是,当我们在笼子里和不在笼子里时,情况检测不好。只需维护一个布尔变量
isInsideCage
,每次通过“[”时将其设置为true,每次通过“]”时将其设置为false,就更容易了

您可以使用
farm.indexOf(']',someIndex)
而不是
someIndex+farm.substring(someIndex).indexOf(']')

然而,您的代码不起作用的原因是,您正在检查笼子之间是否有狐狸,以消除鸡,而笼子外的鸡无论如何都应该是死的。具体来说,如果分支
//如果我们在笼子之间的话,…[

删除这张狐狸支票

现在,您的代码正确地删除了第一个笼子之前的所有鸡,因为
poscageend
posCageStarted
都是-1,所以
posCageStarted>poscageend
poscageend>posCageStarted
,但在第一个笼子之后,除非狐狸在那里,否则它会停止删除笼子外的鸡还有

注:在第2行的评论中为2张狐狸脸投票:)

晚上,唯一保证“安全”的鸡都在笼子里[](除非有狐狸和它们一起进了笼子!)

据我所知,如果没有笼子,那么所有的鸡都会死,不管是不是狐狸,也是如此

//If there are not cages all chickens are cleared
if(!farm.contains("[") && !farm.contains("]")){
  return farm.replace("C",".");
}
应替换为

       if(betweenCages.contains("F")){
代码的另一个问题是,当我们在笼子里和不在笼子里时,情况检测不好。只需维护一个布尔变量
isInsideCage
,每次通过“[”时将其设置为true,每次通过“]”时将其设置为false,就更容易了

您可以使用
farm.indexOf(']',someIndex)
而不是
someIndex+farm.substring(someIndex).indexOf(']')

然而,您的代码不起作用的原因是,您正在检查笼子之间是否有狐狸,以消除鸡,而笼子外的鸡无论如何都应该是死的。具体来说,如果分支
//如果我们在笼子之间的话,…[

删除这张狐狸支票

现在,您的代码正确地删除了第一个笼子之前的所有鸡,因为
poscageend
posCageStarted
都是-1,所以
posCageStarted>poscageend
poscageend>posCageStarted
,但在第一个笼子之后,除非狐狸在那里,否则它会停止删除笼子外的鸡还有

注:在第2行的评论中投票选出2张狐狸脸:)

所以,要清楚的是——你只是在问你对问题描述有什么误解,因为一些例子的预期结果与你预期的不同?所以,要清楚的是——你只是问你对问题描述有什么误解,因为e一些示例的预期结果与您预期的不同?