【问题标题】:Boolean To Return True or False in Method在方法中返回 True 或 False 的布尔值
【发布时间】:2017-09-16 13:43:52
【问题描述】:

无法弄清楚如何编写我的方法,我尝试的任何东西都会给我编译错误。简而言之,我需要我的方法来遍历我传递下来的布尔数组。找出 False 是否连续出现更多,或者 True 是否连续出现更多。在我提供的数组中,False 显得更连续,因此它应该将 false 返回给 main。我现在完全失去了任何提示?

public class booleanTF
{
   public static void main(String [] args)
   {
      boolean[] guess = {false,true,false,false,false,true,true};




   }
   public static boolean longerTF(boolean[] guess)
   {

   int variable = 0;

   for(int x = 0; x < guess.length; x++)
   {
   if(guess[x]




   }
}

【问题讨论】:

标签: java arrays for-loop boolean


【解决方案1】:

显然不是最有效的方法,但是:

public static boolean longerTF(boolean[] guess) {
    return findMaxSeqLength(guess, true) > findMaxSeqLength(guess, false);
}

private static int findMaxSeqLength(boolean[] array, boolean value) {
    int maxSeqLength = 0;
    int counter = 0;

    for (boolean b : array) {
        counter = (b == value) ? ++counter : 0;
        maxSeqLength = Math.max(maxSeqLength, counter);
    }

    return maxSeqLength;
}

找到最长的true序列,找到最长的false序列并比较它们的长度。

【讨论】:

    【解决方案2】:

    public static void booleanTF(){

        boolean[] arr = {true,true,false,false,false};
        int fIndex = 0;
        int fMaxCount = 0;
        int tIndex = 0;
        int tMaxCount = 0;
        for(boolean ar : arr){
            if(ar){
                tIndex++;
                if(tIndex > tMaxCount){
                    tMaxCount = tIndex;
                }
                fIndex= 0;
    
            }else{
                fIndex++;
                if(fIndex > fMaxCount){
                    fMaxCount = fIndex;
                }
                tIndex=0;
            }
        }
        if(fMaxCount > tMaxCount){
            System.out.println("False appers more " + fMaxCount);
        }else if (tMaxCount > fMaxCount){
            System.out.println("True appers more " + tMaxCount);
        }else{
            System.out.println("Same Count " + tMaxCount);
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用标志来检查当前元素是真还是假,以及数组的前一个元素是什么。 您可以尝试以下方法。有什么疑问可以问我

      public class booleanTF {
      public static void main(String[] args) {
          boolean[] guess = { false, true, false, false, false, true, true };
      
          boolean result = longerTF(guess);
          System.out.println(result);
      }
      
      public static boolean longerTF(boolean[] guess) {
      
          int variable = 0;
          boolean trueFlag = false;
          boolean falseFlag = false;
          int trueCount = 0, falseCount = 0;
          for (int x = 0; x < guess.length; x++) {
              if (guess[x] == true) {
                  if (falseFlag == true) {
                      trueCount = 0;
                  }
                  trueFlag = true;
                  falseFlag=false;
                  trueCount++;
              } else {
                  if (trueFlag == true) {
                      falseCount = 0;
                  }
                  falseFlag = true;
                  trueFlag=false;
                  falseCount++;
              }
          }
          if (trueCount > falseCount) {
              return true;
          } else {
              return false;
              }
          }
         }
      

      【讨论】:

        猜你喜欢
        • 2016-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-21
        • 2020-06-23
        • 2015-01-22
        相关资源
        最近更新 更多