y = x + 3 (x > 0)
y = 0 (x = 0)
y = x² - 1 (x < 0)
yx = 0 而非 x == 0intxypublic class PiecewiseFunction {
public static void main(String[] args) {
int x = 3; // 測試值
int y = 0;
if (x > 0) {
y = getYa(x);
} else if (x == 0) {
y = 0;
} else {
y = getYb(x);
}
System.out.println("y = " + y);
}
public static int getYa(int x) {
return x + 3;
}
public static int getYb(int x) {
return x * x - 1;
}
}
y 只聲明一次==public static int getYa(int x)