简体   繁体   English

一个非常简单的Java问题

[英]A really simple Java question

I'm trying to call angles from from the angle method down below in the Rotate90 method but I'm not sure of the syntax. 我正在尝试从Rotate90方法中的角度方法调用角度,但我不确定语法。 What is the correct syntax? 什么是正确的语法?

import java.lang.Math;
public class CartesianPoint implements Point
{
    private double xCoord;
    private double yCoord;

  public CartesianPoint (double xCoordinate,double yCoordinate)
  {
      xCoord = xCoordinate;
      yCoord = yCoordinate;
  }

public double xCoordinate ()
 {
     return xCoord; 
 }

 public double yCoordinate ()
 {
     return yCoord;
 }

 public double angle ()
 {
    double angles;
    angles = Math.cos( xCoord / Math.sqrt( xCoord*xCoord + yCoord*yCoord)); 
    return angles;
 }

 public double radius ()
 {
    double radius;
    radius = (yCoord*yCoord + xCoord*xCoord); //?
    return radius;
 }

public Point rotate90()
{
  double rotated;
  rotated = angles.angle + 90.0; //██████████ Error here ██████████
  return rotated;
}

public double distanceFrom(Point other)
{
  return 0;
}

} }

I think you mean 我想你的意思是

rotated = angle() + 90.0;

Except I think you'll find that Math.cos uses radians not degrees, so you're not going to get the result you think you are. 除了我认为你会发现Math.cos使用弧度而不是度数,所以你不会得到你认为的结果。 And shouldn't that be arc cos, not cosine? 不应该是电弧cos,而不是余弦? Something like this might be more what you're looking for: 这样的事情可能更像你正在寻找的东西:

public double angle()
{
  return Math.atan2(ycoord, xcoord) * 180 / Math.PI;
}

If you want rotate90 to return a new Point that is 90 degrees from the current point, then change it to the following: 如果您希望rotate90返回一个与当前点成90度的新Point,则将其更改为以下内容:

   public Point rotate90()
   {
      return new CartesianPoint(-yCoord, xCoord);
   }

Java中的方法调用总是有尾随括号,即使它们没有任何参数:

rotated = angle() + 90.0;

Method call requires parenthesis even if there are no parameters needed. 即使不需要参数,方法调用也需要括号。 This is different from other languages, eg groovy . 这与其他语言不同,例如groovy

To answer your question directly: If you want to be able to access the variable "angles" from outside the "angle" function, you must make it a class variable. 直接回答您的问题:如果您希望能够从“角度”函数外部访问变量“角度”,则必须使其成为类变量。 That is, declare it outside of any function, like you have declared xCoord and yCoord. 也就是说,在任何函数之外声明它,就像你声明了xCoord和yCoord一样。 Then refer to it by its simple name without any reference to the function that created it. 然后通过它的简单名称引用它,而不引用创建它的函数。

Note that if rotate90 tries to reference angle before angles() is called, angle will contain zero (or whatever default value you set) and is unlikely to be useful. 请注意,如果rotate90在调用angles()之前尝试引用角度,则angle将包含零(或您设置的任何默认值),并且不太可能有用。

It's not clear to me exactly what you're trying to accomplish. 我不清楚你到底想要完成什么。 I think that your intent is that the angles function would take the arc cosine, not the cosine. 我认为你的意图是角度函数将采用反余弦,而不是余弦。

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

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