我在弄清楚如何找到数组中的下一个最大数字时遇到了麻烦。例如;如果我从数组[1,2,3,4,5,7,4,6]中得到数字5,我怎么发现6是下一个最大数字
答案 0 :(得分:0)
基本上这里有两个选项:
请将此视为自己进行编码的灵感。您正是通过这样做来学习编程,而不是将繁重的工作委派给其他人。
答案 1 :(得分:-1)
我认识你了
int numGiven = 0;
//assign number that is given.
numGiven = 5;
int nextHighest = 0;
int[] arrayNum = {1,2,3,4,5,7,4,6};
boolean nextHighestNumberIsFound = false;
boolean endOfLoop = false;
while(endOfLoop == false && nextHighestNumberIsFound == false)
{
++numGiven;
for(int x = 0; x < arrayNum.length; x++)
{
if(numGiven == arrayNum[x])
{
nextHighestNumberIsFound = true;
endOfLoop = true;
nextHighest = arrayNum[x];
x += arrayNum.length;
}
}
endOfLoop = true;
}
if(nextHighest != 0){
System.out.println(nextHighest);
}
else
{
System.out.println("NumberSpecified is too high");
}