Java 调用静态方法时初始化实例

Java 调用静态方法时初始化实例,java,class,static,Java,Class,Static,我想做的是创建一个方法,可以找到两点之间的中点。我还试图显示这些中点的象限。问题是,在将坐标更改为中点后以及在显示点之前,它无法再次正确设置象限。这是我的密码: public class OrderedPairTest { public static void main(String[] args) { OrderedPair op1 = new OrderedPair(0,0); OrderedPair op2 = new OrderedPair(0

我想做的是创建一个方法,可以找到两点之间的中点。我还试图显示这些中点的象限。问题是,在将坐标更改为中点后以及在显示点之前,它无法再次正确设置象限。这是我的密码:

public class OrderedPairTest {


    public static void main(String[] args) {
        OrderedPair op1 = new OrderedPair(0,0);
        OrderedPair op2 = new OrderedPair(0,0);
        OrderedPair op3 = new OrderedPair(0,0);
        System.out.println(op1.a);
        System.out.println(op1.b);
        System.out.println(op1.setX(2));
        System.out.println(op1.setY(3));
        System.out.println(op2.moveX(-1));
        System.out.println(op2.moveY(-3));
        System.out.println(op3.moveXY(-4,4));
        System.out.println(op1.printOP());
        System.out.println(op2.printOP());
        System.out.println(op3.printOP());
        System.out.println(op1.distance(op2));
        System.out.println(op1.distance(op3));
        System.out.println("The distance between the two points is: "+op1.distancestat(op2,op3));
        System.out.println("The midpoint is at: "+op1.midpoint(op2)+op2.q);
        System.out.println("The midpoint is at: "+op2.midpointstat(op1,op3)+op2.q);     
    }
}   

public class OrderedPair {
    int a,b;
    public OrderedPair(int x,int y){
        a = x;
        b = y;
    }
    String q = "";
    String msg = "";
    String error = "";
    String result = "";
    public int getX() {                                                                                 //Gets the value of X
        result = "X is "+a+".";
        return a;
    }
    public int getY() {                                                                                 //Gets the value of Y
        result = "Y is "+b+".";
        return b;   
    }
    public String setX(int x) {                                                                         //Sets the value of X
        a = x;
        result = "X has been set to "+a;        
        if (b != 0) setQ();
        else;
        return result;  
    }
    public String setY(int y) {                                                                         //Sets the value of Y
        b = y;
        result = "Y has been set to "+b;        
        if (a != 0) setQ();
        else;
        return result;  
    }        
    public String toString() {                                                                          //Turns the variables into a string
        msg = ("("+a+","+b+") Q"+q);
        result = "Values have been converted into string.";
        return msg;     
    }
    public String moveX(int amt) {                                                                          //Moves X a predefined amount of units
        a+=amt;
        result = "X has been moved "+amt+" units.";     
        if ((b != 0) && (a != 0)) setQ();
        else;   
        return result;  
    }
    public String moveY(int amt) {                                                                          //Moves Y a predefined amount of units
        b+=amt;
        result = "Y has been moved "+amt+" units."; 
        if ((b != 0) && (a != 0)) setQ();
        else;
        return result;          
    }
    public String moveXY(int amt1, int amt2){                                                               //Moves X and Y a predefined amount of units
        a+=amt1;
        b+=amt2;
        result = "X has been moved "+amt1+" units.Y has been moved "+amt2+" units.";
        if ((b != 0) && (a != 0)) setQ();
        else;
        return result;
    }
    public String distance(OrderedPair other) {                                                         //Gets the distance between two pairs of choordinates
        double d = Math.sqrt(Math.pow(other.b-this.b,2)+Math.pow(other.a-this.a,2));
        result = "The distance between the two points is: "+d;
        return result;
    }
    public static double distancestat(OrderedPair other1, OrderedPair other2) {                                 //Gets the distance between two pairs of static choordinates
        double d = Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other1.b,2));
        return d;
    }
    public OrderedPair midpoint(OrderedPair other) {                                                            //Gets the midpoint between two pairs of choordinates
        return new OrderedPair((this.getX()+other.getX())/2,(this.getY()+other.getY())/2);          
    }
    public static OrderedPair midpointstat(OrderedPair other1, OrderedPair other2) {                                                                                                //Gets the midpoint between two pairs of static choordinates
        return new OrderedPair((other1.getX()+other2.getX())/2,(other1.getY()+other2.getY())/2);        
    }    
    public String printOP() {                                                                               //Calls toString() then prints the string set by toString()
        toString();
        if (q!= "") return msg;
        else error = "Q has not been set.";
        return error;
    } 
    private String setQ() {                                                                             //Sets the quadrant based on the signs of the choordinates
        if ((a!=0)&&(b!=0)){
            if (a > 0){
                if (b > 0) q = "I";     
                else q = "IV";
            }
            else {
                if (b > 0) q = "II";
                else q = "III";
            }       
            result = "The quadrant has been set.";
            return result;
        }
        else error = "X and/or Y are zero, please assign the variables a nonzero value.";
        return error;   
    }            
}
  • 在中点计算中除以2而不是2.0是有问题的

  • 无论是在setX()、setY()还是setQ()中,使用
    正确处理a或b等于零的条件,如果a==0或b==0,则q=”“

  • distancestat()

  • double d=Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other2.a,2))//更改在末尾-other2.a

    恶习


    double d=Math.sqrt(Math.pow(other1.b-other2.b,2)+Math.pow(other1.a-other1.b,2))

    这听起来是学习使用调试器的绝佳机会。请仅显示相关代码,这太多了。你能澄清一下你的标题是什么意思吗?@Jeroenvanevel所有的代码都是相关的,我包括了测试类和其他类。是的,标题到底是什么意思?标题似乎与问题无关,或者你是否可以分享其他细节来澄清这一点?对于代码,您能否在调试器中逐步检查您的逻辑,并更具体地确定观察到的行为偏离预期行为的地方?现在你把它当作一个黑匣子,本质上是说,“它不工作,修复它。”帮助我们帮助你。