我不確定如何正確使用帶有隻返回字符的if/else語句的公共靜態方法。該程序應該帶上x,y並返回座標所在的象限。 (A小白到Java!)無法爲象限檢查程序創建適當的方法
import javax.swing.JOptionPane;
public class Assignment13 {
public static void main(String[] args) {
String userInputx,
userInputy;
double x, y, answer;
userInputx = JOptionPane.showInputDialog("Please enter your x coordinate.");
x = Double.parseDouble(userInputx);
userInputy = JOptionPane.showInputDialog("Please enter your y coordinate.");
y = Double.parseDouble(userInputy);
answer = MethodQuad.quadrant(x, y);
System.out.println("The coordinates " + x + y + "are located Quadrant " + answer);
}
}
class MethodQuad {
public static double quadrant(double x, double y) {
if (x > 0 && y > 0) {
return System.out.println("1");
} else if (x < 0 && y > 0) {
return System.out.println("2");
} else if (x < 0 && y < 0) {
return System.out.println("3");
} else if (x < 0 && y > 0) {
return System.out.println("4");
} else {
return System.out.println("0");
}
}
}
剛一說明:我覺得一個象限功能,可以返回五個* *值之一有效座標是有點...不必要的複雜和任意數學。除非你被告知要這麼做,否則我會建議使用> = 0和<0,那麼軸和原點也算在象限中。 – Boann