我不确定如何将公共静态方法与仅返回字符的if / else语句一起使用。该程序应该取x,y并返回坐标所位于的象限。 (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");
        }
    }
}

最佳答案

它的工作方式类似于另一种编程语言。如果您写了返回值,则必须返回一些值)

 class MethodQuad {
 public static int quadrant(double x, double y)
 {

   if(x > 0 && y > 0)
     return 1;
   else if(x < 0 && y > 0)
     return 2;
   else if(x < 0 && y < 0)
     return 3;
   else if (x<0 && y >0)
     return 4;
   else
     return 0;
  }
}

10-08 13:44