我试图在我创建的数组中找到最大的平台。出于某种原因,我一直挂在 Ln 28 上。我没有看到或纠正什么?
import java.util.Random;
public class MyAttemptAtPlateauProblem
{
public static void main(String[] args)
{
Random random = new Random();
boolean plateau = false;
int largest = 0;
int largest_start = 0;
int array[] = new int[500];
for (int a = 1; a<array.length; a++)
{
array[a]=random.nextInt(20)+1;
System.out.println(array[a]);
}
for ( int a = 1; a < array.length - 1; a++)
if (!plateau && array[a] == array[a+1] && array[a-1]<array[a])
{
plateau = true;
int start = a-1;
}else if (plateau && array[a]==array[a+1])
{
}else if (plateau && array[a]!=array[a+1])
{
if(array[a+1]<array[a])
{
int end = a+1;
if(end- int start+1>largest || (end-start+1==largest && array[largest_start+1]<array[start+1]))
{
largest=end-start+1;
largest_start=start;
}
}
plateau=false;
}
System.out.println(largest_start + " : " + largest + " : ");
for(int a=0;a<largest;a++)
{
System.out.println(array[largest_start+a] + " - ");
}
System.out.println(array);
}
}
我尝试在循环中将 start 声明为一个新变量,但这什么也没做,我认为这样做会丢失以前的值。