应该与OOP的概念有关,JAVA是老外教的,听不太懂。甚至都没什么基础哈哈
public class McmLength {
private int m;
private int cm;
private int hm;
public McmLength() {
}
public McmLength(int hm) {
this.hm = hm;
}
public McmLength(double cm) {
this.cm = (int) cm;
}
public McmLength(int m, int cm, int hm) {
this.m = m;
this.cm = cm;
this.hm = hm;
}
public int add(int x, int y) {
return x + y;
}
public int sub(int x, int y) {
return x - y;
}
public double z(int x, int y) {
return x / y;
}
public int s(int x, int y) {
return x * y;
}
public int Max(int x, int y) {
return Math.max(x, y);
}
public static void main(String[] args) {
McmLength M1 = new McmLength();
McmLength M2 = new McmLength(2);
McmLength M3 = new McmLength(2.3);
McmLength M4 = new McmLength(1, 2, 3);
System.out.println(M1);
System.out.println(M2);
System.out.println(M3);
System.out.println(M4.add(1, 2));
System.out.println(M4.sub(3, 4));
System.out.println(M4.z(5, 6));
System.out.println(M4.s(7, 8));
System.out.println(M4.Max(9, 0));
}
}
你这题目都是翻译过来的吧?
class McmLength{
private int m;
private int cm;
private int mm;
public McmLength(int mm){
this.mm = mm;
}
public McmLength(double cm){
this.cm = (int) cm;
}
public McmLength(int m, int cm, int mm){
this.m = m;
this.cm = cm;
this.mm = mm;
}
public McmLength(){
this.m = this.cm = this.mm = 0;
}
public McmLength add(McmLength a){
int temp = m * 1000 + cm*10 + mm + a.m*1000 + a.cm*10 + a.mm;
McmLength res = new McmLength();
res.m = Math.floorDiv(temp, 1000);
res.cm = Math.floorDiv(temp-res.m * 1000, 10);
res.mm = temp - res.m*1000- res.cm*10;
return res;
}
}
写了个加法,剩下的可以自己练习的写下。
有帮助点个采纳吧