如何将double数组转换为int数组

时间:2014-11-13 10:32:31

标签: java arrays for-loop methods type-conversion

大家好,我的名字是Fyree,我最近完成了一项学校任务,我需要一些帮助。首先,让我向您提供此问题所涉及的代码的后半部分:

public class Rate_Graph_Graph extends Rate_Graph1{
       double Aa, Bb, Cc, Dd, Ee, Ff;
       public void anotheractionPerformed(ActionEvent ae)
       {
          if (ae.getSource() == calc){
            Aa = Double.parseDouble(bv1.getText());
            Bb = Double.parseDouble(ev_1.getText());
            Cc = Double.parseDouble(ev_2.getText());
            Dd = Double.parseDouble(ev_3.getText());
            Ee = Double.parseDouble(ev_4.getText());
            Ff = Double.parseDouble(ev_5.getText());
          }
       }
       JLabel graphLabel;
       double[] data = {Aa, Bb, Cc, Dd, Ee, Ff};
       final int finalInt = 50;
       public void paintGraph(Graphics g){
       super.paintComponent(g);
       Graphics2D g2 = (Graphics2D)g;

       int w = getWidth();
       int h = getHeight();

       graphLabel = new JLabel ("<HTML><FONT SIZE =+25>Line Graph showing the Return Rate</FONT>");
       add(graphLabel);

       g2.draw(new Line2D.Double(finalInt, finalInt, finalInt, h-finalInt));
       g2.draw(new Line2D.Double(finalInt, h-finalInt, w-finalInt, h-finalInt));
       Font font1 = g2.getFont();
       FontRenderContext fontRender = g2.getFontRenderContext();
       LineMetrics lineMetrics = font1.getLineMetrics("0", fontRender);
       float ft = lineMetrics.getAscent()+lineMetrics.getDescent();
       String str = "";

       g.drawString("Aa", 5, 315);
       g.drawString("Bb", 5, 260);
       g.drawString("Cc",5,205);
       g.drawString("Dd",5,150);
       g.drawString("Ee",5,100);
       g.drawString("Ff",5,55);

       g.drawString("Beginning Value",50,335);
       g.drawString("Yeatr 1 Value",175,335);
       g.drawString("Year 2 Value",300,335);
       g.drawString("Year 3 Value",425,335);
       g.drawString("Year 4 Value",550,335);
       g.drawString("Year 5 Value",675,335);

       float ft2 = finalInt + ((h - 2*finalInt) - str.length()*ft)/2 + lineMetrics.getAscent();
       for(int i = 0; i < str.length(); i++){
        String strLetter = String.valueOf(str.charAt(i));
        float ft3 = (float)font1.getStringBounds(strLetter, fontRender).getWidth();
        float ft4 = (finalInt - ft3)/2;
        g2.drawString(strLetter, ft3, ft4);
        ft2 += ft;
       }
       str = "YEARS";
       ft2 = h - finalInt + (finalInt - ft)/2 + lineMetrics.getAscent();
       float ft3 = (float)font1.getStringBounds(str, fontRender).getWidth();
       float ft4 = (w - ft3)/2;
       g2.drawString(str, ft4, ft2);
       double xInc = (double)(w - 2*finalInt)/(data.length-1);
       double scale = (double)(h - 2*finalInt)/getMax();
       g2.setPaint(Color.green.darker());
       for(int i = 0; i < data.length-1; i++){
           double x1 = finalInt + i*xInc;
           double y1 = h - finalInt - scale*data[i];
           double x2 = finalInt + (i+1)*xInc;
           double y2 = h - finalInt - scale*data[i+1];
           g2.draw(new Line2D.Double(x1, y1, x2, y2));
        }
   }
   private double getMax()
   {
    int max = -Integer.MAX_VALUE;
    final int[] intData = new int[data.length];
    for(int i = 0; i < data.length; i++){
       if(data[i] > max)
        max = data[i];
    }
    return max;
   }
   public static void main(String[]args)
   {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new GraphingData());
    f.setSize(800,400);
    f.setLocation(200,200);
    f.setResizable(false);
    f.setVisible(true);
   }
}

现在我的问题是,如何将上面的double []数据数组转换为int数组,以便我能够在私有双getMax()方法中使用它代码的结尾(我得到了错误:&#34;不兼容的类型:从double到int&#34的转换可能有损;当我尝试在没有转换的情况下执行此操作时)?任何帮助都表示赞赏,因为这项任务将于今天晚些时候到期!

1 个答案:

答案 0 :(得分:0)

你的错误就在这里

private double getMax()
{
int max = -Integer.MAX_VALUE;
final int[] intData = new int[data.length];
for(int i = 0; i < data.length; i++){
   if(data[i] > max)
    max = data[i];
}
return max;

}

你的数组是双倍的,你将它与int进行比较,你应该这样做:

private double getMax()
{
double max = Double.MIN_VALUE;
final int[] intData = new int[data.length];
for(int i = 0; i < data.length; i++){
   if(data[i] > max)
    max = data[i];
}
return max;

}

顺便说一下,什么是intData,它永远不会被使用?