查找数组中最大的6个双精度数并存储为双

时间:2016-01-03 04:05:37

标签: c++ arrays

我有一个结构,让我们称之为竞争对手。在这里,有一个Double类型的数组,保持竞争对手跳跃的6个高度。还有一个名为" bestJump"然后它将保持最高跳跃,但必须计算。

6个跳转将作为报告打印到.txt文件,以及最佳跳转,此信息也将显示在控制台中。

void bestJump(competitor comp)
{
    comp.bestJump = 0;

    for (int i = 0; i <= 5; i++)
    {
        if (comp.jump[i] > comp.bestJump)
        {
            comp.bestJump = comp.jump[i];
        }
    }
}

此代码不会返回错误,但也不会返回数组中最大的double。相反,它返回     -92559631349317830000000000000000000000000000000000000000000000.00

我哪里出错或者我没有做过什么?

3 个答案:

答案 0 :(得分:3)

另一个有点整理的答案:

void bestJump(competitor &comp)
{
    comp.bestJump = comp.jump[0];
    for (int i = 1; i < 6; ++i)
    {
        if (comp.jump[i] > comp.bestJump)
            comp.bestJump = comp.jump[i];
    }
}

此方法接受引用作为函数参数,如Cornstalks建议的那样。因此.bestJump在原始对象中被修改。

答案 1 :(得分:0)

很确定你想传递comp in的地址,然后执行:

void bestJump(competitor * comp)
{
    comp->bestJump = 0;

    for (int i = 0; i <= 5; i++)
    {
        if (comp->jump[i] > comp->bestJump)
        {
            comp.bestJump = comp.jump[i];
        }
    }
}

答案 2 :(得分:0)

最好将对象作为参考传递。

你所做的是传递一个对象本身的副本(准确地传递值)因此,即使从函数返回时my_btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ImageIcon pic = new ImageIcon(); JLabel lbl = new JLabel(); if(counter == 0) { pic = new ImageIcon(c1); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 1) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(c2); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 2) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(c3); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 3) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(c4); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 4) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(c5); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 5) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(c6); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 6) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(c7); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 7) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(c8); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 8) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(c9); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 9) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(c10); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 10) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(cj); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 11) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(cq); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else if(counter == 12) { frame.remove(lbl); lbl.setVisible(false); pic = new ImageIcon(ck); lbl = new JLabel(pic); frame.add(lbl); lbl.setBounds(200, 100, 100, 100); lbl.setVisible(true); counter++; } else { frame.remove(lbl); lbl.setVisible(false); JOptionPane.showMessageDialog(frame, "Card deck ends."); } } }); 分配了正确的值,对象也会重置为状态当函数被调用时。

您甚至可以尝试将函数comp.bestJump = comp.jump[i]定义为bestJump类的朋友。