我的任务是创建主类,在其中我将任何点的值初始化为(0,0,0)并且能够分别访问和改变所有三个值(x,y,z).为此,我使用了getter和setter.我的下一个任务是在我的主类(我称之为"distanceTo")中创建一个计算两点之间距离的方法.
如何创建方法" distanceTo",通过获取x,y,z坐标来计算两点之间的距离?我假设我的答案将与某些事情有关,sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)但我不知道如果我的点在我的第二个测试点类之前没有定义,我怎么能在我的主类中的方法中编写它
到目前为止我只有两点,但我正在寻找一个更一般的答案(所以如果我创建三个点,p1 p2和p3,我可以计算p1和p2之间的距离或p2和p3之间的距离或距离在p1和p3之间.
我的主要课程:
package divingrightin;
public class Point3d {
private double xCoord;
private double yCoord;
private double zCoord;
public Point3d(double x, double y, double z){
xCoord = x;
yCoord = y;
zCoord = z;
}
public Point3d(){
this (0,0,0);
}
public double getxCoord() {
return xCoord;
}
public void setxCoord(double xCoord) {
this.xCoord = xCoord;
}
public double getyCoord() {
return yCoord;
}
public void setyCoord(double yCoord) {
this.yCoord = yCoord;
}
public double …Run Code Online (Sandbox Code Playgroud) 所以我有一个名为Person的类,看起来像这样
public class Person {
private String personName;
public String toString(){
return personName;
}
public Person(String personName){
this.personName = personName;
}
}
Run Code Online (Sandbox Code Playgroud)
和另一个我正在创建对象的类
public class IdanJavaTask {
public static void main(String[] args) {
Person p1 = new Person("Bob");
System.out.println("p1 : " + p1);
Person p2 = new Person("Joe");
System.out.println("p2 :" + p2);
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切都很好,我的打印声明是
p1:Bob
p2:Joe
现在我想创建一个新对象,p3并将其设置为p1,我的类现在看起来像这样:
public class IdanJavaTask {
public static void main(String[] args) {
Person p1 = new Person("Bob");
System.out.println("p1 : " + p1);
Person p2 …Run Code Online (Sandbox Code Playgroud) 我编写了一个程序,它接受Java中三个整数的总和和范围.我的程序有几个if语句用于查找范围(最大和最小整数之间的距离).我想知道我的"if语句"是否是找到范围的最有效方法,如果没有,那么我怎样才能使我的"if语句"对我的代码更有效,以及我应该考虑哪些事情?我正在编写if和else if,以及Java中的语句.
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer value for n1");
n1 = in.nextInt();
System.out.println("The (integer) value for n1 is " + n1);
System.out.println("Enter an integer value for n2");
n2 = in.nextInt();
System.out.println("The (integer) value for n2 is " + n2);
System.out.println("Enter an integer value for n3");
n3 = in.nextInt();
System.out.println("The (integer) value for n3 is " + n3);
sum = n1 + n2 + n3;
System.out.println("The sum of n1+n2+n3 = " + sum);
if(n3< n1 && n3<n2){ …Run Code Online (Sandbox Code Playgroud) 我有一个计算闰年的程序,但它只能在1750年左右开始工作,所以我希望我的用户只能在1750年之后输入数字.
System.out.println("Enter a year after 1750:");
leapYear = in.nextInt();
if(leapYear<1750){
System.out.println("You have entered a year before 1750, please try again :");
leapYear = in.nextInt();
}
Run Code Online (Sandbox Code Playgroud)
我的解决方案是提出一个if语句.然而,我意识到,只有当用户输入一次低于1750的东西时,这才有效.如果他们再次这样做,程序将继续.有没有办法可以执行声明"你已经在1750年之前进入了一年,请再试一次:"我需要多次而不重写它?
我想也许一个while语句可以工作(如果一个while语句工作怎么能这样做而不会导致无限循环)?
我有这个代码:
char str [80];
int i;
printf ("Enter your family name: ");
scanf ("%79s",str);
printf ("Enter your age: ");
scanf ("%d",&i);
Run Code Online (Sandbox Code Playgroud)
为什么scanf()的string保存str而对于int它被发送到pointer of i?
java ×4
if-statement ×2
arrays ×1
c ×1
coordinate ×1
distance ×1
equals ×1
leap-year ×1
methods ×1
pointers ×1
scanf ×1
while-loop ×1