好吧,基本上我有一个客户端,它显示数组的中位数(如果它是有序的) 这是在服务器中检查的, 如果数组不按顺序排列,则会生成“错误消息”,但不会生成中位数。 然而,尽管我创建了两个条件,以便只有 IF(boolean = true 那么它显示中位数),但它仍然继续处理数组中位数的值
下面是我正在谈论的部分的客户端'
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 + ".");
}
}}
'
以下是来自服务器的方法。 if( (数组[i+1] < 数组[i]) || ((数组[i+1] > 数组[i] && (数组[0] > 数组[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;
}
'
这是我从 Java 得到的响应
假
错误,数字未按顺序排列
数组的中位数是 5.5。 <----这不应该在那里!
最佳答案
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 + ".");
}
错误...使用
if(trueFalse) //bad way of naming a boolean variable BTW
'=' 将值 false/true 分配给 truefalse...它不检查..
关于java - 如果语句没有调用正确的字符串或 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20673727/