0

数组在正确的位置初始化,其他一切看起来都很好。我只是无法弄清楚为什么当我打印完成的数组时它会使所有元素都相同。

当我调试它时,它将新元素存储在数组上的一个新位置,但它不会创建该对象的新实例。

数组初始化:

public class CalcFrame implements ActionListener
{
   private static Calculation[] calc_array = new Calculation[10];

在行动...

if(ae.getSource() == calc_answer_position)
{ 
      calc_array[count] = new Calculation();
      calc_array[count].setIntVel(Double.parseDouble(init_vel_tf.getText())); 
      calc_array[count].setIntPos(Double.parseDouble(init_pos_tf.getText()));
      calc_array[count].setAccel(Double.parseDouble(acc_tf.getText()));
      calc_array[count].setTime(Double.parseDouble(time_tf.getText()));


      double ans = getArray(count).calcPosition(getArray(count).getIntVel(),
                   getArray(count).getIntPos(), getArray(count).getAccel(),
                    getArray(count).getTime());
      count++;

      int n = JOptionPane.showOptionDialog(null, "Position x(t) = " + ans,
              "Answer: Calculate Position", JOptionPane.YES_NO_OPTION,
                 JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

      if (n == JOptionPane.YES_OPTION)
      {
          frame.dispose();
      }
          if (n == JOptionPane.NO_OPTION)
      {
          System.exit(0);
      } 
}

计算类....编辑

public class Calculation 
{
   //These are the attributes for the Variables object.
   private static double initial_accel = 0.0,
                                 initial_velocity = 0.0,
                                 initial_position = 0.0,
                                 acceleration = 0.0,
                                 velocity = 0.0,
                                 position = 0.0,
                                 time = 0.0,
                                 height = 0.0,
                                 mass = 0.0,
                                 weight = 0.0,
                                 answer = 0.0;


    public Calculation() {}

    public Calculation(double initial_velocity, double initial_position, double acceleration, double time)
    {
        setIntVel(initial_velocity);
        setIntPos(initial_position);
        setAccel(acceleration);
        setTime(time);
    }

    public Calculation(double initial_velocity, double initial_position, double acceleration, double time, double position)
    {
        setIntVel(initial_velocity);
        setIntPos(initial_position);
        setAccel(acceleration);
        setPos(position);
        setTime(0);
    }

    public static double calcPosition(double in_vel, double in_pos, double acc, double time)
    {
       //x(t) = xi + vi(t) + .5(a)t^2

        double answer = .5 * time * time * acc;
        answer = (in_pos + (in_vel * time) + answer);
       return answer;
    }

    public static double calcTime(double in_vel, double in_pos, double acc, double pos)                 
    {
       //t = (-vi + sqrt(vi^2 - 2(a)xi)) / a

        double answer = (in_vel * in_vel) - (2 * acc * (in_pos - pos));
      answer = ((-1 * in_vel) + Math.sqrt(answer)) / (acc);
       return answer;
    }

    //===MUTATORS===//

    public void setIntAccel(double initial_accel) 
    {
       this.initial_accel = initial_accel;
    }

    public void setIntVel(double initial_velocity) 
    {
       this.initial_velocity = initial_velocity;
    }

    public void setIntPos(double initial_position) 
    {
       this.initial_position = initial_position;
    }

    public void setAccel(double acceleration) 
    {
       this.acceleration = acceleration;
    }

    public void setVel(double velocity) 
    {
       this.velocity = velocity;
    }

    public void setPos(double position) 
    {
       this.position = position;
    }

    public void setTime(double time) 
    {
       this.time = time;
    }

    public void setHeight(double height) 
    {
       this.height = height;
    }

    public void setMass(double mass) 
    {
       this.mass = mass;
    }

    public void setWeight(double weight) 
    {
       this.weight = weight;
    }

    public void setAnswer(double answer) 
    {
       this.answer = answer;
    }


    //===ACCESSORS===//

    public double getIntAccel()
    {
       return initial_accel;
    }

    public double getIntVel() 
    {
       return initial_velocity;
    }

    public double getIntPos() 
    {
       return initial_position;
    }

    public double getAccel()
    {
       return acceleration;
    }

    public double getVel()
    {
       return velocity;
    }

    public double getPos()
    {
       return position;
    }

    public double getTime()
    {
       return time;
    }

    public double getHeight()
    {
       return height;
    }

    public double getMass()
    {
       return mass;
    }

    public double getWeight() 
    {
       return weight;
    }

    public double getAnswer() 
    {
       return answer;
    }

    public String toString()
    {

        String result = "Initial Position: " + getIntPos();
        result += "\nAcceleration: " + getAccel();
        result += "\nInitial Velocity: " + getIntVel();
        result += "\nPosition: " + getPos();
        result += "\nTime: " + getTime();
        return result;
    }


}

CalcFrame 类...编辑

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;

public class CalcFrame implements ActionListener
{
   private static Calculation[] calc_array = new Calculation[10];  

    private JLabel init_vel_label,
                   init_pos_label,
                        acc_label,
                        time_label,
                        pos_label;

    private JButton calc_answer_time,
                    calc_answer_position;

    private static JFrame frame;

    private JTextField init_vel_tf,
                       init_pos_tf,
                             acc_tf,
                             time_tf,
                             pos_tf;

    private static int count = 0;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the main GUI components.
   //-----------------------------------------------------------------
   public CalcFrame(int operation)
   {
      if(operation == 2)
           CalcTime();
        if(operation == 1)
           CalcPosition();
   }

    public void CalcTime()
   {
      frame = new JFrame ("Quadratic Calculator");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        Toolkit tk = Toolkit.getDefaultToolkit();
       Dimension d = tk.getScreenSize();

        init_vel_label = new JLabel ("Initial Velocity");
        init_pos_label = new JLabel ("Initial Position");
        acc_label = new JLabel ("Acceleration");
        pos_label = new JLabel ("Position at time (t)");

        //textfields
      init_vel_tf = new JTextField (10);
        init_pos_tf = new JTextField (10);
        acc_tf = new JTextField (10);
        pos_tf = new JTextField (10);

        //button
        calc_answer_time = new JButton("Calculate");
      calc_answer_time.addActionListener(this);


      frame.add (init_vel_label);
        frame.add (init_vel_tf);
        frame.add (init_pos_label);
        frame.add (init_pos_tf);
        frame.add (acc_label);
        frame.add (acc_tf);
        frame.add (pos_label);
        frame.add (pos_tf);
        frame.add (calc_answer_time);


        frame.setSize(new Dimension(200, 275));
      frame.pack();
        frame.setResizable(false);
        frame.setLocation((d.width/2)-380, (d.height/2)-200);
      frame.setVisible(true);
      frame.setBackground (new Color(0,0,156));
        frame.getContentPane();
   }

    public void CalcPosition()
   {
      frame = new JFrame ("Quadratic Calculator");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        Toolkit tk = Toolkit.getDefaultToolkit();
       Dimension d = tk.getScreenSize();

        init_vel_label = new JLabel ("Initial Velocity");
        init_pos_label = new JLabel ("Initial Position");
        acc_label = new JLabel ("Acceleration");
        time_label = new JLabel ("Time (or change in time)");

        //textfields
      init_vel_tf = new JTextField (10);
        init_pos_tf = new JTextField (10);
        acc_tf = new JTextField (10);
        time_tf = new JTextField (10);

        //button
        calc_answer_position = new JButton("Calculate");
      calc_answer_position.addActionListener(this);


      frame.add (init_vel_label);
        frame.add (init_vel_tf);
        frame.add (init_pos_label);
        frame.add (init_pos_tf);
        frame.add (acc_label);
        frame.add (acc_tf);
        frame.add (time_label);
        frame.add (time_tf);
        frame.add (calc_answer_position);


        frame.setSize(new Dimension(200, 275));
      frame.pack();
        frame.setResizable(false);
        frame.setLocation((d.width/2)-380, (d.height/2)-200);
      frame.setVisible(true);
      frame.setBackground (new Color(0,0,156));
        frame.getContentPane();
   }

    public static void sort()
    {
         int i = 0;
         int j = 0;
         Calculation k;
         for(i = getSize() - 1; i >= 0; i--)
         {
             for(j = 0; j <= i - 1; j++)
              {
                  if(getArray(j).getIntVel() > getArray(j + 1).getIntVel())
                    {
                        k = getArray(j);
                         setArray(j, getArray(j + 1));
                         setArray(j + 1, k);
                    }
              }
         }
    }

    public static Calculation getArray(int i)
    {
       return calc_array[i];
    }

    public static Calculation[] getEntireArray()
    {
       return calc_array;
    }

    public static void setArray(int i, Calculation c)
    {
       calc_array[i] = c;
    }

    public static int getSize()
    {
       return count;
    }


    public void actionPerformed(ActionEvent ae)
    { 
          Object[] options = {"Main Menu",
                                "Exit"};

      if(ae.getSource() == calc_answer_position)
      { 
         calc_array[count] = new Calculation();
          calc_array[count].setIntVel(Double.parseDouble(init_vel_tf.getText())); 
          calc_array[count].setIntPos(Double.parseDouble(init_pos_tf.getText()));
          calc_array[count].setAccel(Double.parseDouble(acc_tf.getText()));
          calc_array[count].setTime(Double.parseDouble(time_tf.getText()));


          double ans = getArray(count).calcPosition(getArray(count).getIntVel(),
                       getArray(count).getIntPos(), getArray(count).getAccel(),
                        getArray(count).getTime());
          count++;

          int n = JOptionPane.showOptionDialog(null, "Position x(t) = " + ans,
                  "Answer: Calculate Position", JOptionPane.YES_NO_OPTION,
                     JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

                     if (n == JOptionPane.YES_OPTION)
                     {
                         frame.dispose();
                     }
                     if (n == JOptionPane.NO_OPTION)
                     {
                         System.exit(0);
                     }


        }

          if(ae.getSource() == calc_answer_time)
          { 

              calc_array[count] = new Calculation(Double.parseDouble(init_vel_tf.getText()), 
                       Double.parseDouble(init_pos_tf.getText()), Double.parseDouble(acc_tf.getText()),
                          0, Double.parseDouble(pos_tf.getText()));


              double ans = getArray(count).calcTime(getArray(count).getIntVel(), getArray(count).getIntPos(), getArray(count).getAccel(), getArray(count).getPos());
              count++;

              int n = JOptionPane.showOptionDialog(null, "Time t = " + ans, "Answer: Calculate Time", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

                         if (n == JOptionPane.YES_OPTION)
                         {
                             frame.dispose();
                         }
                         if (n == JOptionPane.NO_OPTION)
                         {
                             System.exit(0);
                         }


          }

   }
}

我认为这就是问题所需要的全部内容,如果您需要更多信息,请告诉我,我会待命。

再次感谢你

更新代码如下....

唯一的修复是删除 Calculation 类中的静态关键字和更改对数组的调用。

例子:

getArray(j).getIntVel() changed to ... calc_array[j].getIntVel()

计算类已更新...

public class Calculation 
{
   //These are the attributes for the Variables object.
   private double initial_accel = 0.0,
                                 initial_velocity = 0.0,
                                 initial_position = 0.0,
                                 acceleration = 0.0,
                                 velocity = 0.0,
                                 position = 0.0,
                                 time = 0.0,
                                 height = 0.0,
                                 mass = 0.0,
                                 weight = 0.0,
                                 answer = 0.0;


    public Calculation() {}

    public Calculation(double initial_velocity, double initial_position, double acceleration, double time)
    {
        setIntVel(initial_velocity);
        setIntPos(initial_position);
        setAccel(acceleration);
        setTime(time);
    }

    public Calculation(double initial_velocity, double initial_position, double acceleration, double time, double position)
    {
        setIntVel(initial_velocity);
        setIntPos(initial_position);
        setAccel(acceleration);
        setPos(position);
        setTime(0);
    }

    public double calcPosition(double in_vel, double in_pos, double acc, double time)
    {
       //x(t) = xi + vi(t) + .5(a)t^2

        double answer = .5 * time * time * acc;
        answer = (in_pos + (in_vel * time) + answer);
       return answer;
    }

    public double calcTime(double in_vel, double in_pos, double acc, double pos)                    
    {
       //t = (-vi + sqrt(vi^2 - 2(a)xi)) / a

        double answer = (in_vel * in_vel) - (2 * acc * (in_pos - pos));
      answer = ((-1 * in_vel) + Math.sqrt(answer)) / (acc);
       return answer;
    }

    //===MUTATORS===//

    public void setIntAccel(double initial_accel) 
    {
       this.initial_accel = initial_accel;
    }

    public void setIntVel(double initial_velocity) 
    {
       this.initial_velocity = initial_velocity;
    }

    public void setIntPos(double initial_position) 
    {
       this.initial_position = initial_position;
    }

    public void setAccel(double acceleration) 
    {
       this.acceleration = acceleration;
    }

    public void setVel(double velocity) 
    {
       this.velocity = velocity;
    }

    public void setPos(double position) 
    {
       this.position = position;
    }

    public void setTime(double time) 
    {
       this.time = time;
    }

    public void setHeight(double height) 
    {
       this.height = height;
    }

    public void setMass(double mass) 
    {
       this.mass = mass;
    }

    public void setWeight(double weight) 
    {
       this.weight = weight;
    }

    public void setAnswer(double answer) 
    {
       this.answer = answer;
    }


    //===ACCESSORS===//

    public double getIntAccel()
    {
       return initial_accel;
    }

    public double getIntVel() 
    {
       return initial_velocity;
    }

    public double getIntPos() 
    {
       return initial_position;
    }

    public double getAccel()
    {
       return acceleration;
    }

    public double getVel()
    {
       return velocity;
    }

    public double getPos()
    {
       return position;
    }

    public double getTime()
    {
       return time;
    }

    public double getHeight()
    {
       return height;
    }

    public double getMass()
    {
       return mass;
    }

    public double getWeight() 
    {
       return weight;
    }

    public double getAnswer() 
    {
       return answer;
    }

    public String toString()
    {

        String result = "Initial Position: " + getIntPos();
        result += "\nAcceleration: " + getAccel();
        result += "\nInitial Velocity: " + getIntVel();
        result += "\nPosition: " + getPos();
        result += "\nTime: " + getTime();
        return result;
    }


}

CalcFrame 类已更新...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;

public class CalcFrame implements ActionListener
{
   private static Calculation[] calc_array = new Calculation[10];  

    private JLabel init_vel_label,
                   init_pos_label,
                        acc_label,
                        time_label,
                        pos_label;

    private JButton calc_answer_time,
                    calc_answer_position;

    private static JFrame frame;

    private JTextField init_vel_tf,
                       init_pos_tf,
                             acc_tf,
                             time_tf,
                             pos_tf;

    private static int count = 0;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the main GUI components.
   //-----------------------------------------------------------------
   public CalcFrame(int operation)
   {
      if(operation == 2)
           CalcTime();
        if(operation == 1)
           CalcPosition();
   }

    public void CalcTime()
   {
      frame = new JFrame ("Quadratic Calculator");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        Toolkit tk = Toolkit.getDefaultToolkit();
       Dimension d = tk.getScreenSize();

        init_vel_label = new JLabel ("Initial Velocity");
        init_pos_label = new JLabel ("Initial Position");
        acc_label = new JLabel ("Acceleration");
        pos_label = new JLabel ("Position at time (t)");

        //textfields
      init_vel_tf = new JTextField (10);
        init_pos_tf = new JTextField (10);
        acc_tf = new JTextField (10);
        pos_tf = new JTextField (10);

        //button
        calc_answer_time = new JButton("Calculate");
      calc_answer_time.addActionListener(this);


      frame.add (init_vel_label);
        frame.add (init_vel_tf);
        frame.add (init_pos_label);
        frame.add (init_pos_tf);
        frame.add (acc_label);
        frame.add (acc_tf);
        frame.add (pos_label);
        frame.add (pos_tf);
        frame.add (calc_answer_time);


        frame.setSize(new Dimension(200, 275));
      frame.pack();
        frame.setResizable(false);
        frame.setLocation((d.width/2)-380, (d.height/2)-200);
      frame.setVisible(true);
      frame.setBackground (new Color(0,0,156));
        frame.getContentPane();
   }

    public void CalcPosition()
   {
      frame = new JFrame ("Quadratic Calculator");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        Toolkit tk = Toolkit.getDefaultToolkit();
       Dimension d = tk.getScreenSize();

        init_vel_label = new JLabel ("Initial Velocity");
        init_pos_label = new JLabel ("Initial Position");
        acc_label = new JLabel ("Acceleration");
        time_label = new JLabel ("Time (or change in time)");

        //textfields
      init_vel_tf = new JTextField (10);
        init_pos_tf = new JTextField (10);
        acc_tf = new JTextField (10);
        time_tf = new JTextField (10);

        //button
        calc_answer_position = new JButton("Calculate");
      calc_answer_position.addActionListener(this);


      frame.add (init_vel_label);
        frame.add (init_vel_tf);
        frame.add (init_pos_label);
        frame.add (init_pos_tf);
        frame.add (acc_label);
        frame.add (acc_tf);
        frame.add (time_label);
        frame.add (time_tf);
        frame.add (calc_answer_position);


        frame.setSize(new Dimension(200, 275));
      frame.pack();
        frame.setResizable(false);
        frame.setLocation((d.width/2)-380, (d.height/2)-200);
      frame.setVisible(true);
      frame.setBackground (new Color(0,0,156));
        frame.getContentPane();
   }

    public static void sort()
    {
         int i = 0;
         int j = 0;
         Calculation k;
         for(i = count - 1; i >= 0; i--)
         {
             for(j = 0; j <= i - 1; j++)
              {
                  if(calc_array[j].getIntVel() > calc_array[j+1].getIntVel())
                    {
                        k = calc_array[j];
                         calc_array[j] = calc_array[j+1];
                         calc_array[j + 1] = k;
                    }
              }
         }
    }

    public static Calculation getArray(int i)
    {
       return calc_array[i];
    }

    public Calculation[] getEntireArray()
    {
       return calc_array;
    }

    public void setArray(int i, Calculation c)
    {
       calc_array[i] = c;
    }

    public static int getSize()
    {
       return count;
    }


    public void actionPerformed(ActionEvent ae)
    { 
          Object[] options = {"Main Menu",
                                "Exit"};

      if(ae.getSource() == calc_answer_position)
      { 
         calc_array[count] = new Calculation();
          calc_array[count].setIntVel(Double.parseDouble(init_vel_tf.getText())); 
          calc_array[count].setIntPos(Double.parseDouble(init_pos_tf.getText()));
          calc_array[count].setAccel(Double.parseDouble(acc_tf.getText()));
          calc_array[count].setTime(Double.parseDouble(time_tf.getText()));


          double ans = getArray(count).calcPosition(getArray(count).getIntVel(),
                       getArray(count).getIntPos(), getArray(count).getAccel(),
                        getArray(count).getTime());
          count++;

          int n = JOptionPane.showOptionDialog(null, "Position x(t) = " + ans,
                  "Answer: Calculate Position", JOptionPane.YES_NO_OPTION,
                     JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

                     if (n == JOptionPane.YES_OPTION)
                     {
                         frame.dispose();
                     }
                     if (n == JOptionPane.NO_OPTION)
                     {
                         System.exit(0);
                     }


        }

          if(ae.getSource() == calc_answer_time)
          { 

              calc_array[count] = new Calculation(Double.parseDouble(init_vel_tf.getText()), 
                       Double.parseDouble(init_pos_tf.getText()), Double.parseDouble(acc_tf.getText()),
                          0, Double.parseDouble(pos_tf.getText()));


              double ans = getArray(count).calcTime(getArray(count).getIntVel(), getArray(count).getIntPos(), getArray(count).getAccel(), getArray(count).getPos());
              count++;

              int n = JOptionPane.showOptionDialog(null, "Time t = " + ans, "Answer: Calculate Time", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

                         if (n == JOptionPane.YES_OPTION)
                         {
                             frame.dispose();
                         }
                         if (n == JOptionPane.NO_OPTION)
                         {
                             System.exit(0);
                         }


          }

   }
}
4

2 回答 2

4

你能提供计算类吗?这可能是一个静态问题


编辑:

好的尝试在你的变量声明中删除静态

那里

//These are the attributes for the Variables object.
   private **static** double initial_accel = 0.0,

您还必须在这些变量出现的所有方法上删除静态

认为静态变量和/或方法是独立的,它们不依赖于当前实例,这意味着任何静态属性在同一实例的对象之间总是相同的,您甚至可以在没有实例的情况下冷藏到这些属性

Person person1 = new Person();
person1.name = "User";
Person person2 = new Person()
person2.name = "Admin";

如果 name 是静态的,那么 person1.name 将永远是 person2.name,否则他们将有自己的名字

于 2012-05-16T21:48:10.803 回答
2

您是否在代码中的任何位置调用方法 sort()。因为我看到下面的内部排序方法

setArray(j, getArray(j + 1));
setArray(j + 1, k);

因为 您正在通过方法Calculation k;从数组中分配引用。getArray(int i)由于您正在参考参考,因此这里可能会发生一些事情。这只是一个猜测。

于 2012-05-16T22:02:21.793 回答