0

到目前为止我有这个:

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();
}

这几乎适用于所有情况,除非输入的数字都是降序的。示例:如果您输入 5,4,3,2,1,您会得到 5,4,3 作为答案,而您应该得到 1,2,3。

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

有任何想法吗?

4

6 回答 6

1
    if (dval[a] == high)
    {
        high = a+1;
    }
    if (dval[a] == sec)
    {
        sec = a+1;
    }
    if (dval[a] == thr)
    {
        thr = a+1;
    }

当您确定它们的索引时,您正在重用相同的变量。在 5, 4, 3, 2, 1 情况下,首先将高位设置为 1,稍后将匹配内容。

引入 3 个新变量highIndsecIndthrInd,这应该可以解决您的问题。

在 for 循环之上:

int highInd=0;
int secInd=0;
int thrInd=0;

在 for 循环中:

if (dval[a] == high)
{
    highInd = a+1;
}
if (dval[a] == sec)
{
    secInd = a+1;
}
if (dval[a] == thr)
{
    thrInd = a+1;
}

试试这个。打印时,将变量名称更改为这些。

于 2013-09-09T19:38:05.473 回答
1

此块可能有问题,因为您将 , 和从表示数组的值重新用于high表示sec数组thr的索引。

不仅如此,您还依赖high,secthr, 是整个循环中数组的值。

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;
    }
}

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

但是,当您进行第二次迭代时, and highis 1, and a, is 1,条件:(dval[a] == high)将为真,但错误,并且在整个循环中会发生类似的错误。

强烈建议使用不同的变量来跟踪你的价值观的指数,而不是你用来跟踪你的价值观的那些。

于 2013-09-09T19:41:46.213 回答
0

我花了一点时间来写这篇文章,但为什么不一次处理所有的处理。一口气获取您的索引值和实际值。如果您找到一个更大的值并继续通过 for 循环运输,则将值向下滚动。

这段代码没有经过全面测试,但更多的是一个示例,在您找到更大的值时显示滚动值。

int firstLargest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
int thirdLargest = Integer.MIN_VALUE;

int firstLargestIndex = -1;
int secondLargestIndex = -1;
int thirdLargestIndex = -1;

// loop through array, check for a higher value than values 
// that have already been saved, if necessary, roll the values down
// and save the current value
for (j = 0; j < dval.length; j++) { 
    if(dval[j] > firstLargest) {
        thirdLargestIndex = secondLargestIndex;
        secondLargestIndex = firstLargestIndex;
        firstLargestIndex = j;

        thirdLargest = secondLargest;
        secondLargest = firstLargest;
        firstLargest = dval[j];
    } else if(dval[j] > secondLargest) {
        thirdLargestIndex = secondLargestIndex;
        secondLargestIndex = j;

        thirdLargest = secondLargest;
        secondLargest = dval[j];
    } else if(dval[j] > thirdLargest) {
        thirdLargestIndex = j;

        thirdLargest - dval[j];
    }
}
于 2013-09-09T19:38:27.817 回答
0

如果您对替代方法感兴趣,可以创建Map将数字映射到其索引的映射。例如

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numList.Length; i++)
    map.put(numList[i], i);
List<Integer> sortedList = // get descending list of keys from map
for (int i = 0; i < 3; i++)
    System.out.println(String.valueOf(numList.get(i) + 1));

我更喜欢这个解决方案,因为它更短,因此恕我直言,更具可读性和易于调试。它可能比你的慢一点,因为它让其他类做一些额外的工作。

于 2013-09-09T19:39:49.767 回答
0
  • 创建链表
  • 种类
  • 弹出 3 个第一个项目
  • 在原始数组中找到他们的 id

    Integer data[] = new Integer[] {10,20,30,40,50,60,71,80,90,91 };
    ArrayList<Integer> originalList = new ArrayList<Integer>(Arrays.asList(data));
    LinkedList<Integer> sortedList = new LinkedList<Integer>(Arrays.asList(data));
    
    Collections.sort(sortedList,Collections.reverseOrder());
    
    Integer biggest = sortedList.pop();
    Integer second = sortedList.pop();
    Integer third = sortedList.pop();
    
    Integer indexOfBiggest = originalList.indexOf(biggest);
    Integer indexOfSecond = originalList.indexOf(second);
    Integer indexOfThird = originalList.indexOf(third);
    
于 2013-09-09T20:22:26.757 回答
0

只是想法:

1-使用整数而不是整数

2-为您希望获得前 3 个的排序顺序创建 2 个比较器

3- 使用 LinkedList 和 PriorityQueue 代替数组

坦率地说,我不太明白你的意思,但是为了获取用户输入的前三个,你可以将他输入的值存储在链接列表中并获取前三个

如果您有某个等式来对他的输入进行排序,您可以创建一个比较器(实现比较器接口的类)并在创建优先级队列时使用它的实例(或使用此比较器对列表进行排序)并获得第一个三要素

如果您能进一步说明您的情况,我可以提供帮助

于 2013-09-09T21:19:02.687 回答