请问这个java程序为什么错了,该如何改正?谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/28 06:10:06
public class Leaf {

int i = 0;
Leaf increment(int j) {
i++;
return this;
}
void print(int j) {
System.out.println("i = " + i);
}
public static void main(String[] args) {
Leaf x = new Leaf();
x.increment().increment().increment().print();

}
} ///:~
应该怎么改正呢?谢谢,我是菜鸟

public class Leaf {
int i = 0;
void increment() {
i++;
}

void print() {
System.out.println("i = " + i);
}

public static void main(String[] args) {
Leaf x = new Leaf ();
x.increment();
x.increment();
x.increment();
x.print();
}
}
这样,打印出来i=3
不知道你是不是要这样的答案
你这个程序问题很多,
首先,x.increment().increment().increment().print(); 这句话里的.运算符不能连续调用,每次都要分开用
其次,你在调用方法时,没有传参数,那你写的方法就不要传参了

increment().increment().increment().print();
明显不对