【发布时间】:2017-12-03 18:15:49
【问题描述】:
这是家庭作业,不,我不希望你为我做这件事。我正在参加在线课程以提高我的 Java 技能。我们的任务是创建一个程序,该程序使用 Point.java 类来接受两个坐标并计算距离。然后它将继续询问坐标并告知距离,直到用户退出。我有第一部分下来。我们得到了代码:
public class Point {
private double x;
private double y;
public Point(){
this.x = 0.0;
this.y = 0.0;
}
public Point(double x,double y){
this.x = x;
this.y = y;
}
public double distance(Point pt){
return Math.sqrt((x - pt.x)*(x - pt.x) + (y - pt.y)*(y - pt.y));
}
public double getX(){
return x;
}
public double getY(){
return y;
}
}
如何从给定的坐标创建对象,将它们放入数组列表中,并使用给定的类?我无法接触到真正的教授。计算机在线评分。
这是我目前的代码:
import java.util.Scanner;
public class Assignment{
public static void main(String[] args){
try {
System.out.println("Welcome.");
System.out.println("To quit at anytime please type \"Q\". Enter value for first point X:");
Scanner scan = new Scanner (System.in);
while (scan.hasNext()) {
if (scan.hasNextDouble()) {
double x = scan.nextDouble();
//System.out.println(x); //Printing for verification
System.out.println("Please enter vlaue for first point Y:");
double y = scan.nextDouble();
//System.out.println(y); //Printing for verification
//Point point1 = new Point(x,y);
System.out.println("First point created.");
System.out.println("Please enter value for second point X:");
double x2 = scan.nextDouble();
//System.out.println(x2); //Printing for verification
System.out.println("Please enter value for second point Y:");
double y2 = scan.nextDouble();
//System.out.println(x2); //Printing for verification
break;
}
else {
String input = scan.next();
if (input.equalsIgnoreCase("Q")) {
System.out.println("Exiting");
break;
}
else {
System.out.println("You did not enter a valid value. Please enter a number or \"Q\" to quit.");
}
}
}
}
catch(Exception e){
System.out.println("Exiting Program.");
}
}
}
【问题讨论】:
标签: java arrays object arraylist