[英]Sorted array (ascending or descending) - true or false
This post details how to determine if the given integers of an array (ascending or descending) are sorted, returning an integer representing the array.这篇文章详细介绍了如何确定数组的给定整数(升序或降序)是否已排序,返回代表该数组的 integer。 It also accounts for when given integers are equal (1 1 2 3 3 4 5, for example).它还考虑了给定整数何时相等(例如,1 1 2 3 3 4 5)。
I've been trying to figure out how to change the return values from an integer to a boolean value in the Main with no avail.我一直在试图弄清楚如何在 Main 中将返回值从 integer 更改为 boolean 值,但无济于事。 Meaning, if the array is sorted in any direction, return 'true', if not, 'false'.意思是,如果数组按任意方向排序,则返回“true”,否则返回“false”。 Been working on this problem for the better part of two days now and have learned a lot, but I'm stuck.现在已经在这两天的大部分时间里一直在研究这个问题并且学到了很多东西,但我被困住了。 Please help.请帮忙。
I have attempted System.out.println(isSorted(array.toString("true")));我尝试过 System.out.println(isSorted(array.toString("true"))); to see if that even works, which it does not.看看这是否有效,但它没有。
I have tried switching the method return value from int to boolean and then changing all the integers that would be affected to 'true' or 'false'.我尝试将方法返回值从 int 切换为 boolean,然后将所有会受到影响的整数更改为“true”或“false”。
I have attempted reassigning the array reference in the Main method to a String 'true' or 'false'.我尝试将 Main 方法中的数组引用重新分配给字符串“true”或“false”。
import java.util.Scanner;
import java.util.Arrays;
class Sorted {
public static int isSorted(int[] intArray) {
boolean sortedAsc = true;
boolean sortedDesc = true;
boolean equalValues=true;
int result = 0;
for (int i = 0; i < intArray.length-1; i++)
{
equalValues = equalValues && (intArray[i] == intArray[i+1]);
sortedAscending = sortedAscending && ((intArray[i] <= intArray[i+1]));
sortedDescending = sortedDescending && ((intArray[i] >= intArray[i+1]));
if(sortedAsc) result= 1;
if(sortedDesc) result = -1;
if(equalValues) result = 2;
return result;
}
public static void main(String[] args) {
// TODO code application logic here
int array[] = new int[4];
array[0]=1;
array[1]=2;
array[2]=3;
array[3]=4;
int sortedResult = isSorted(array);
System.out.println( sortedResult == 1 || sortedResult == -1 || sortedResult == 2 );
}
}
If I understand correctly, what you are trying to do is to see if an array is sorted in ascending or descending way or if all the elements are equal in the array ( which is also sorted by the way ).如果我理解正确,您要做的是查看数组是否以升序或降序方式排序,或者数组中的所有元素是否相等(也按方式排序)。 If that is the case I would modify your for
loop as:如果是这种情况,我会将您的for
循环修改为:
for (int i = 0; i < intArray.length-1; i++)
{
equalValues = equalValues && (intArray[i] == intArray[i+1]);
sortedAscending = sortedAscending && ((intArray[i] <= intArray[i+1]));
sortedDescending = sortedDescending && ((intArray[i] >= intArray[i+1]));
}
Basically you need to keep track of your previous values of the booleans to check the invariant of the sorted-ness.基本上,您需要跟踪布尔值的先前值以检查排序的不变量。
If you need strictly ascending or strictly descending, you just need to change <=
to <
and >=
to >
如果您需要严格升序或严格降序,您只需将<=
更改为<
和>=
更改为>
Now in order to print your result as true
based on the result
现在为了根据result
打印你的结果为true
You just need to do:你只需要这样做:
int sortedResult = isSorted(array)
System.out.println( sortedResult == 1 || sortedResult == -1 || sortedResult == 2 )
Based on your update to the code, you have a few errors, just replace the method isSorted
method with the below and try:根据您对代码的更新,您有一些错误,只需将方法isSorted
方法替换为以下方法并尝试:
public static int isSorted(int[] intArray) {
boolean sortedAsc = true;
boolean sortedDesc = true;
boolean equalValues=true;
int result = 0;
for (int i = 0; i < intArray.length-1; i++)
{
equalValues = equalValues && (intArray[i] == intArray[i+1]);
sortedAsc = sortedAsc && ((intArray[i] <= intArray[i+1]));
sortedDesc = sortedDesc && ((intArray[i] >= intArray[i+1]));
}
if(sortedAsc) result= 1;
if(sortedDesc) result = -1;
if(equalValues) result = 2;
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.