如何使用布尔表达式来修复此代码?

时间:2014-02-23 04:04:55

标签: java

我想创建一种方法,从提供的字符串的前面或后面或前后两者中裁剪指定数量的字符。如果要裁剪的字符数太长,给定文本的长度,它将返回“无法裁剪超过文本的长度。”

参数应该执行以下操作:

@param  String text - a piece of text of any length containig ascii characters
@param  int howMany - number of characters to be cropped
@param  boolean front - true if characters should be cropped from the front of text, false don't crop from front
@param  boolean back - true if characters should be cropped from the back of text, false don't crop from back 

我目前有这个代码,但程序崩溃特别是在显示“裁剪(两个):”Say String文本说“sammy”和int howMany说“4”时,if语句不会显示因为int howMany是不大于String文本,但是...当显示裁剪(两者)时程序崩溃,因为无法从前面裁剪4,从后面裁剪4,这导致-3。我确定我必须在这里使用布尔值,但我不确定我应该如何将它们合并,它们是我的参数的一部分,但我没有使用它们。您能否向我解释如何修复此代码以执行所需的任务? main方法调用此方法,并将参数从该方法传递给此方法。我为所有布尔表达式传递了true。这是我的代码...

public static String cropText(String text, int howMany, boolean front, boolean back)
{
  String result = "";

  if (howMany > text.length())
  {
     JOptionPane.showMessageDialog(null,"Can't crop more than the length of the text");
  }

  else
  {
     System.out.println("Cropped (both):");
     System.out.println("-----------");
     System.out.println(text.substring(howMany, text.length() - howMany));
     System.out.println("--------------");

     System.out.println("Cropped (front):");
     System.out.println("-----------");
     System.out.println(text.substring(howMany, text.length() - 0));
     System.out.println("--------------");
     front = true;

     System.out.println("Cropped (back):");
     System.out.println("-----------");
     System.out.println(text.substring(0, text.length() - howMany));
     System.out.println("--------------");
     back = true;
  }

  return result;
}

3 个答案:

答案 0 :(得分:1)

而不是

if (howMany > text.length())

您需要查看是否要求您在零,一侧或双方进行裁剪。

sides = 0;
if (front) sides++;
if (back) sides++;
if(howMany * sides > text.length())

答案 1 :(得分:0)

问题是当您从两个侧进行裁剪时,不会从文本中裁剪howMany,而是2*howMany。所以,你必须改变两件事:

  • 更改if条件:

    if ((front && back && (howMany*2 > text.length())) || howMany > text.length())
    
  • println放入条件块:

    if (front && back) {
        System.out.println("Cropped (both):");
        System.out.println("-----------");
        System.out.println(text.substring(howMany, text.length() - howMany));
        System.out.println("--------------");
    }
    
    if (front) {
        System.out.println("Cropped (front):");
        System.out.println("-----------");
        System.out.println(text.substring(howMany, text.length() - 0));
        System.out.println("--------------");
    }
    
    if (back) {
        System.out.println("Cropped (back):");
        System.out.println("-----------");
        System.out.println(text.substring(0, text.length() - howMany));
        System.out.println("--------------");
    }
    

答案 2 :(得分:0)

到目前为止,您似乎忽略了布尔frontback;那些应该与函数的工作原理相关。解决这个问题的最简单方法可能就是下面的伪代码:

if(front should be cropped) {
    if(front can be cropped) {
        crop front
    } else {
        say you can't
    }
}

same for the back

if(front and back should be cropped) {
    if(front and back can be cropped) {
        crop front and back
    } else {
        say you can't
    }
}

此代码可以进行优化,但应该很容易理解这种方式。