java利用方法重载编程求函数f( )的值,在main()方法中实现x,y,z值的输...
发布网友
发布时间:2024-05-06 01:26
我来回答
共1个回答
热心网友
时间:2024-06-05 15:05
直接看代码:
import java.util.Scanner;
public class What {
public static void main(String[] args) throws Exception {
int x, y, z;
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
in.close();
int result;
if (x < 0) {
result = f();
} else if (x >= 0 && y < 0) {
result = f(x);
} else if (x >= 0 && y >= 0 && z < 0) {
result = f(x, y);
} else {
result = f(x, y, z);
}
System.out.println(result);
}
public static int f() {
return 0;
}
public static int f(int x) {
return x * x;
}
public static int f(int x, int y) {
return x * x + y * y;
}
public static int f(int x, int y, int z) {
return x * x + y * y + z * z;
}
}