我正在尝试做一个作业问题,将极坐标转换为笛卡尔坐标,反之亦然,使用继承和多态性,这也给出了点之间的距离、线的斜率和线的方程,给定输入的两个值用户。
我创建了我的父类,以及 Polar 和 Cartesian 类。我正在尝试 1. 调用我的测试坐标类中的方法(由于某种原因它无法识别),以及 2. 创建一个成功的嵌套循环。
循环情况:我有一个 if 语句导致输入极坐标或笛卡尔坐标。这很好用,一旦输入坐标,就会提示用户选择一个过程(获取线的斜率、线的方程、线之间的距离等)。我不知道如何创建这个嵌套循环。每次我尝试时,一旦我完成内循环,它就会停止循环,并且不会返回到菜单的外循环。
任何见解将不胜感激!
父代码(我无法调用这些方法):
public abstract class Coordinate {
private double value1, value2;
public Coordinate(double val1, double val2) {
this.value1 = value1;
this.value2 = value2;
}
public double getValue1() {
return value1;
}
public double getValue2() {
return value2;
}
public abstract String getEquationOfLine(Coordinate c);
public abstract double getDistance(Coordinate c);
public abstract double getSlopeOfLine(Coordinate c);
}
代码:
import java.util.Scanner;
public class TestCoordinates {
public static String getSelection(Scanner in) {
System.out.println("(p) Polar Coordinates (r, theta (degrees))");
System.out.println("(c) Cartesian Coordinates (x, y)");
System.out.println("(x) Exit program");
System.out.println("What type of coordinates? Please enter p or c, or x to quit. ");
return in.next();
}
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
String coordinateString = getSelection(scan);
while(coordinateString.equalsIgnoreCase("p") || coordinateString.equalsIgnoreCase("c")) {
if (coordinateString.equalsIgnoreCase("p")) {
System.out.println("Coordinate 1 - Please enter the radius: ");
int radius = scan.nextInt();
System.out.println("Coordinate 1 - Please enter theta (degrees): ");
int theta = scan.nextInt();
System.out.println("Coordinate 2 - Please enter the radius: ");
int radius1 = scan.nextInt();
System.out.println("Coordinate 2 - Please enter theta (degrees): ");
int theta1 = scan.nextInt();
System.out.println("[1] Convert to Cartesian coordinates.");
System.out.println("[2] Find the distance between the two points.");
System.out.println("[3] Find the slope of the line between the points.");
System.out.println("[4] Find the equation of the line between the points.");
System.out.println("[5] Return to main menu.");
System.out.println("What would you like to do? Enter the number: ");
int num = scan.nextInt();
/*if (num = 1) {
getCartesian();
System.out.println("The Cartesian coordinate for " + "(" + radius + "," + theta + ") is" + getCartesian);
System.out.println("The Cartesian coordinate for " + "(" + radius1 + "," + theta1 + ") is" + getCartesian);
}*/
/*if (int num = 2){
getDistance();
System.out.println("The distance between the Polar coordinates (" + radius + "," + theta + ") and " + radius1 +"," + theta1 + ") is" getDistance);
}
if (int num = 3) {
getSlopeOfLine;
System.out.println("The slope of the line between the Polar coordinates (" + radius + "," + theta + ") and " + radius1 +"," + theta1 + ") is" getSlopeOfLine);
}
if (int num = 4) {
System.out.println("The equation of the line between the points is: ")
}
else if (num = 5) {
return;
}*/