如何找到数组中最大,第二大和第三大的数字,然后显示其序列位置? [英] How to find the biggest, second biggest and third biggest number in an array, then display their sequence location?

查看:412
本文介绍了如何找到数组中最大,第二大和第三大的数字,然后显示其序列位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到目前为止:

  public static void highV()
{
KeyboardReader reader =新的KeyboardReader();

int numVal = 0;

while(numVal< 3)//确保输入了3个或更多数字
{
numVal = reader.readInt(您要输入多少个值( 3个或更多):);

if(numVal< 3)
{
System.out.println( Invalid Entry);
}
}

int [] dval = new int [numVal];

int i;
int j;
int k;
int a;
int高= 0;
int sec = 0;
int thr = 0;

System.out.println();
for(i = 0; i< dval.length; i ++)//读取数字并将它们存储在数组
{
dval [i] = reader.readInt( Enter数值 +(i + 1)+。);

}



System.out.println();
System.out.print(值列表:);
for(j = 0; j< dval.length; j ++)//打印出值列表
{
if(j ==(dval.length)-1)
{
System.out.println(dval [j]);
}
其他
{
System.out.print(dval [j] +,);
}

}
System.out.println();

System.out.println(总共输入了 + dval.length +个数字。);

System.out.println();

for(k = 0; k< dval.length; k ++)//确定最高的第二高和第三高数字
{
if(dval [k]> ; high)
{
int oldSec = sec;
sec =高;
thr = oldSec;
高= dval [k];
}
否则,如果(dval [k]> sec)
{
thr = sec;
秒= dval [k];
}
否则,如果(dval [k]> thr)
{
thr = dval [k];
}
}


for(a = 0; a< dval.length; a ++)//确定第一个第二和第三高数字的序列位置
{
if(dval [a] == high)
{
high = a + 1;
}
if(dval [a] == sec)
{
sec = a + 1;
}
if(dval [a] == thr)
{
thr = a + 1;
}
}

System.out.println(最高编号按顺序#: +高);
System.out.println(第二高的数字按顺序#: + sec);
System.out.println(第三高的数字按顺序#: + thr);
System.out.println();
}

这几乎适用于所有内容,除非输入的数字全部为降序。示例:如果输入5,4,3,2,1,当您应该获得1,2,3时,您会得到5,4,3作为答案。



如果输入2,18,5,3,1,0,9,100但是您会得到正确的答案8,2,7



有什么想法吗?

解决方案

此块可能有问题,因为您打算重新使用 high sec thr 从表示数组的值到表示



不仅如此,还取决于 high sec thr 是整个循环中数组的值。

  for(a = 0; a< dval.length; a ++)//确定第一个第二和第三高数字
{
if(dval [ a] ==高)
{
高= a + 1;
}
if(dval [a] == sec)
{
sec = a + 1;
}
if(dval [a] == thr)
{
thr = a + 1;
}
}






第一次迭代后,将为 5 ,(正确),但是将其设置为 1 您要在输出中显示。



但是当您进行第二次迭代时, 1 ,而 a 1 ,条件为:(dval [a] ==高)是正确的,但错误的是,在整个循环中也会发生类似的错误。 / p>

我会强烈建议使用与跟踪值不同的变量来跟踪值的索引。


I have this so far:

public static void highV()
{
    KeyboardReader reader = new KeyboardReader();

    int numVal = 0;

    while (numVal < 3) // Makes sure 3 or more numbers are entered
    {
        numVal = reader.readInt("How many values would you like to enter (3 or more): ");

        if (numVal < 3)
        {
            System.out.println("Invalid Entry");
        }
    }

    int[] dval = new int[numVal];

    int i;
    int j;
    int k;
    int a;
    int high = 0;
    int sec = 0;
    int thr = 0;

    System.out.println();
    for (i = 0; i < dval.length; i++) // Reads in numbers and stores them in an array
    {
        dval[i] = reader.readInt("Enter value number " + (i + 1) + ". ");

    }



    System.out.println();
    System.out.print("List of values: "); 
    for (j = 0; j < dval.length; j++)// Prints out a list of values
    {
        if (j == (dval.length)-1)
        {
            System.out.println(dval[j]);
        }
        else
        {
            System.out.print(dval[j] + ", ");
        }

    }
    System.out.println();

    System.out.println("There was a total of " + dval.length + " numbers entered.");

    System.out.println();

    for (k = 0; k < dval.length; k++) // Determines the highest second highest and third highest numbers
    {
        if (dval[k] > high)
        {
            int oldSec = sec;
            sec = high;
            thr = oldSec;
            high = dval[k];
        }
        else if (dval[k] > sec)
        {
            thr = sec;
            sec = dval[k];
        }
        else if (dval[k] > thr)
        {
            thr = dval[k];
        }
    }


    for (a = 0; a < dval.length; a++) // Determines sequence location of first second and third highest numbers
    {
        if (dval[a] == high)
        {
            high = a+1;
        }
        if (dval[a] == sec)
        {
            sec = a+1;
        }
        if (dval[a] == thr)
        {
            thr = a+1;
        }
    }

    System.out.println("The highest number was in sequence #: " + high);
    System.out.println("The second highest number was in sequence #: " + sec);
    System.out.println("The third highest number was in sequence #: " + thr);
    System.out.println();
}

This works for almost everything, except when the numbers entered are all descending. Example: If you enter 5,4,3,2,1 you get 5,4,3 as answers when you should get 1,2,3.

If you enter 2,18,5,3,1,0,9,100 however you get the correct answer of 8,2,7

Any ideas?

解决方案

this block might be problematic because you're repurposing high, sec, and thr from representing the values of the array to representing the index of the array.

Not only that, but you're depending on high, sec, and thr, being values of the array throughout the loop.

for (a = 0; a < dval.length; a++) // Determines sequence location of first second and third highest numbers
{
    if (dval[a] == high)
    {
        high = a+1;
    }
    if (dval[a] == sec)
    {
        sec = a+1;
    }
    if (dval[a] == thr)
    {
        thr = a+1;
    }
}


after the first iteration, high will be 5,(correct), but you'll set it to 1 which you want to display in your output.

But when you come through the second iteration, and high is 1, and a, is 1, the condition: (dval[a] == high) will be true, but in error, and similar erros will happen throughout that loop.

I would Strongly advise using different variables to keep track of the indices of your values than the ones that you use to keep track of your values.

这篇关于如何找到数组中最大,第二大和第三大的数字,然后显示其序列位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆