我试图在用户创建对象时从x,y获得以度为单位的alpha角度.
我写了以下构造函数:
public class Point
{
private double _radius , _alpha;
public Point ( int x , int y )
{
_radius = Math.sqrt ( Math.pow(x,2) + Math.pow (y,2) ) ;
_alpha = ( ( Math.atan (y/x) ) * 180 ) / Math.PI;
}
}
Run Code Online (Sandbox Code Playgroud)
我是对的,_alpha现在是一个角度,而不是我从atan()方法得到的弧度?
有一个简单的方法吗?
谢谢 !
有什么区别:
Math.pow ( x,y ); // x^y
Run Code Online (Sandbox Code Playgroud)
至:
x^y; // x^y
Run Code Online (Sandbox Code Playgroud)
?
我会更喜欢使用x^y带double式号码?或者shell我必须总是使用Math.pow()方法?
如何构建一个接收另一个点(x,y)并复制其值的复制构造函数?
我决定签名:public Point1 (Point1 other)但我不知道写些什么...
Point类看起来像:
public class Point1
{
private int _x , _y;
public Point1 (Point1 other)
{
...
...
}
//other more constructors here...
}
Run Code Online (Sandbox Code Playgroud)
我试过了:
public Point1 (Point1 other)
{
_x = other._x ;
_y = other._y;
}
Run Code Online (Sandbox Code Playgroud)
但我几乎可以肯定我能做得更好..
日Thnx
我编写了以下构造函数,它获取了2个参数,如果值(x或y)为负,它将初始化为零.
public Point1 ( int x , int y )
{
//if one or more of the point values is <0 , the constructor will state a zero value.
if (x < 0)
{
_x = 0;
}
else
_x=x;
if (y < 0)
{
_y = 0;
}
else
_y = y;
}
Run Code Online (Sandbox Code Playgroud)
如果它可以......我只需要它是极简主义
我从我的大学看了我的讲师的视频,他说Rational的构造函数是这样的:
Rational (int top=0 , int bottom=1)
: t(top) , b(bottom) {normalize();}
Run Code Online (Sandbox Code Playgroud)
到现在为止一切都还可以,但是!! 他还说你只能用1个参数(top参数)调用构造函数,并且因为底部初始化为1,所以理性例如:Rational(3)将是3/1.
但是!! 我想知道如果它只支持2个参数,我们怎么能使用一个值为1的构造函数呢?
我知道在java中,如果我们有构造函数接收的x个参数(不考虑其他构造函数而x> 0),我们必须将它们全部转移而不是1而不是2 ...
请帮我解决这个冲突......
日Thnx ...
我写了以下代码:
public class Point2
{
private double _radius , _alpha;
public Point2 ( int x , int y )
{
//if one or more of the point values is <0 , the constructor will state a zero value.
if (x < 0)
{
x = 0;
}
if (y < 0)
{
y = 0;
}
_radius = Math.sqrt ( Math.pow(x,2) + Math.pow (y,2) ) ;
_alpha = Math.toDegrees( Math.atan ((double)y/x) );
}
public Point2 (Point2 other) // copy constructor …Run Code Online (Sandbox Code Playgroud) 我用Java写了一个'setX'方法,如果x值((x,y))是负数,x的值就不会改变,而main()会像往常一样继续.
void setX (int num)
{
if (num < 0)
break;
else
{
_x = num;
}
}
Run Code Online (Sandbox Code Playgroud)
我对吗 ?我不确定因为中断问题,break语句刚刚从当前方法中断?
日Thnx