简体   繁体   English

如何将双精度数组转换为整数数组

[英]How to convert a double array into a int array

Hello everyone my name is Fyree, I have recently been finishing up a school assignment, and am in need of some assistance. 大家好,我叫Fyree,我最近正在完成学校作业,需要一些帮助。 First let me give you the second half of my code to which this question pertains: 首先,让我给您介绍与该问题有关的代码的后半部分:

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

Now here is what my question is, how do you I convert the double[] data array that I have above into an int array so that I am able to use it in the private double getMax() method that is at the end of the code (I get the error: "incompatible types: possible lossy of conversion from double to int" when I try to do it without the conversion)? 现在这是我的问题是,如何将上面的double []数据数组转换为int数组,以便可以在末尾的私有double getMax()方法中使用它。代码(当我尝试不进行转换时,出现错误:“不兼容的类型:可能从双精度转换为整数的损失”)? Any help is appreciated as this assignment is due later today! 感谢您的帮助,因为此作业将于今天晚些时候到期!

your mistake is here 你的错误在这里

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;

} }

Your array is double, and you are comparing it to int, you should do like this: 您的数组是double,并且您正在将其与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;

} }

Btw, what is intData, it is never used? 顺便说一句,什么是intData,它从未使用过?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM