简体   繁体   English

如果语句未调用正确的字符串或布尔值

[英]If statements aren't calling correct Strings or Booleans

Okay so basically I have a client that displays the median of an array IF it is in order This is checked in the server, IF the array is not in order then it produces an "Error message"but not the median. 好的,所以基本上我有一个客户端,如果按顺序显示了数组的中位数,则在服务器中检查该数组,如果该数组不按顺序,则它会生成“错误消息”,但不显示中位数。 however it keeps processing the value of the median of the array although I created two conditions so that only IF(boolean = true then it displays the median) 但是,尽管我创建了两个条件,所以它继续处理数组的中值。

Below is the client at the part in which I'm talking about' 以下是我正在谈论的部分中的客户'

        double median = Server.getMedian(array);

        Boolean truefalse = Server.checkFalse(array);
        // prints the response if the array is in order or not
        String response = Server.errorMsg(array);
        System.out.println(truefalse);
        if(truefalse = false){
            System.out.println(response);
        }
        else if(truefalse = true){
            System.out.println(response);
            System.out.println("The median of the array is " + median + ".");
        }
    }}
'

Here are the methods from the server. 这是来自服务器的方法。 if( (array[i+1] < array[i]) || ((array[i+1] > array[i] && (array[0] > array[9]))) ){ This line above checks for ascending and descending order' if((array [i + 1] <array [i])||((array [i + 1]> array [i] &&(array [0]> array [9])))){上面的检查升序和降序”

 public static String errorMsg(int[] array){
            String checker = "Error, the numbers are not in order";
            for(int i =0; i<array.length-1; i++){
                if( (array[i+1] < array[i]) || ((array[i+1] > array[i] && (array[1] > array[9]))) ){
                    return checker;
                }
            }
            return "The numbers are in order!";  
        }



        public static Boolean checkFalse(int[] array){
            for(int i =0; i<array.length-1; i++){
 if( (array[i+1] < array[i]) || ((array[i+1] > array[i] && (array[1] > array[9]))) ){
                    return false;
                }
            }
            return true;  
        }



    public static double getMedian(int[] array){
        double median;
        if (array.length%2 == 0){
    median = ((double)(array[(array.length/2)-1] + (double)array[array.length/2])/2);
        }
        else{
            median = (array[(array.length/2)-1]);
        }
        return median;
    }
'

This is what I get as responses from Java 这是我从Java得到的回应

false

Error, the numbers are not in order 错误,数字不正确

The median of the array is 5.5. 数组的中位数为5.5。 <---- this should not be there! <----这不应该在那里!

if(truefalse = false){
            System.out.println(response);
        }
        else if(truefalse = true){
            System.out.println(response);
            System.out.println("The median of the array is " + median + ".");
        }

is wrong... use 是错的...使用

if(trueFalse)  //bad way of naming a boolean variable BTW

'=' assigns the value false/true to truefalse... It doesnt check.. '='将值false / true分配给truefalse...。它不检查..

== is used when comparing values. 比较值时使用==

= is the assignment operator, not a comparison operator. =是赋值运算符,不是比较运算符。

try == : 尝试==

if(truefalse == false)

= is assignment, not equivalence. =是分配,而不是等价。

Now, you could switch all of your statements that look like: 现在,您可以切换所有如下所示的语句:

if(truefalse = false)

to: 至:

if(truefalse == false)

...but I would propose a better way. ...但是我会提出一种更好的方法。 Since you already have some kind of boolean, why not just use it directly? 既然您已经有了某种布尔值,为什么不直接使用它呢?

if(!truefalse)  //shorthand for truefalse is false

if(truefalse) //shorthand for truefalse is true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM