定义一个类,该类具有x和y两个静态变量,定义构造方法初始化这两个变量,在定义四个方法,分别求出这两个数的和,差,乘,除出结果并输出在(main)方法中,用户能输入两个数字。
时间: 2023-05-19 18:04:42 浏览: 485
好的,以下是代码:
```java
public class MyClass {
static int x;
static int y;
public MyClass(int a, int b) {
x = a;
y = b;
}
public static int add() {
return x + y;
}
public static int subtract() {
return x - y;
}
public static int multiply() {
return x * y;
}
public static int divide() {
return x / y;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入两个数字:");
int a = scanner.nextInt();
int b = scanner.nextInt();
MyClass myClass = new MyClass(a, b);
System.out.println("和为:" + MyClass.add());
System.out.println("差为:" + MyClass.subtract());
System.out.println("积为:" + MyClass.multiply());
System.out.println("商为:" + MyClass.divide());
}
}
```
希望这个代码能够帮到你!
阅读全文