Java 我必须通过MyObject进行的练习有问题
我有一个练习:定义一个递增类来从主类获取代码 给出了结果:Java 我必须通过MyObject进行的练习有问题,java,iteration,Java,Iteration,我有一个练习:定义一个递增类来从主类获取代码 给出了结果: package incr; import static incr.Incrementer.*; public class Test { public static void main(String[] args) { // simplest iteration - step = 1 for(int k : in(1, 10)) System.out.print(k + " &quo
package incr;
import static incr.Incrementer.*;
public class Test {
public static void main(String[] args) {
// simplest iteration - step = 1
for(int k : in(1, 10)) System.out.print(k + " ");
System.out.println();
// The given step
for(int k : in(1, 10).by(2)) System.out.print(k + " ");
System.out.println();
// It can be the other way around - step = -1 by default
for(int k : in(10, 1)) System.out.print(k + " ");
System.out.println();
// But the range can be made from min to max and the given step will be
// decide on the iteration direction
for(int k : in(1, 10).by(-1)) System.out.print(k + " ");
System.out.println();
// During the iteration, you can change the step
Incrementer inc;
for (int i : inc = in(1,10) ) {
if (i == 4) inc.by(2);
System.out.print(i + " ");
}
System.out.println();
for (int i : inc = in(1,10) ) {
if (i == 8) inc.by(-2);
System.out.print(i + " ");
}
System.out.println();
for(int k : inc = in(10, 1)) {
if (k == 5) inc.by(1);
System.out.print(k + " ");
}
}
}
1 2 3 4 5 6 7 8 9 10
1 3 5 7 9
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
1 2 3 4 6 8 10
1 2 3 4 5 6 7 8 6 4 2
10 9 8 7 6 5 6 7 8 9 10
给出了结果:
package incr;
import static incr.Incrementer.*;
public class Test {
public static void main(String[] args) {
// simplest iteration - step = 1
for(int k : in(1, 10)) System.out.print(k + " ");
System.out.println();
// The given step
for(int k : in(1, 10).by(2)) System.out.print(k + " ");
System.out.println();
// It can be the other way around - step = -1 by default
for(int k : in(10, 1)) System.out.print(k + " ");
System.out.println();
// But the range can be made from min to max and the given step will be
// decide on the iteration direction
for(int k : in(1, 10).by(-1)) System.out.print(k + " ");
System.out.println();
// During the iteration, you can change the step
Incrementer inc;
for (int i : inc = in(1,10) ) {
if (i == 4) inc.by(2);
System.out.print(i + " ");
}
System.out.println();
for (int i : inc = in(1,10) ) {
if (i == 8) inc.by(-2);
System.out.print(i + " ");
}
System.out.println();
for(int k : inc = in(10, 1)) {
if (k == 5) inc.by(1);
System.out.print(k + " ");
}
}
}
1 2 3 4 5 6 7 8 9 10
1 3 5 7 9
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
1 2 3 4 6 8 10
1 2 3 4 5 6 7 8 6 4 2
10 9 8 7 6 5 6 7 8 9 10
要求:
包含Incrementer类的程序不能使用表或集合
提示:
在。。。而且。。。是递增类中的方法
递增器应该实现Iterable接口
我的问题是:当主测试类中没有define Incrementer时,如何迭代方法?!->forint k:in1,10 System.out.printk+ 我认为您应该能够使用如下所示的定制Iterable类来实现这一点。基本上,我在Incrementer中添加了以下内容: Incrementer有一个静态方法,其中将接收范围开始和结束的两个整数,以及迭代器的返回和实例。 在迭代器方法中,我们将返回一个Interator,它将简单地将步长值添加到当前值并返回。 hasNext将确保下一个传入值保持在范围边界内。 Incrementer还有一个by方法,允许修改其成员变量step。开始和结束的值将根据步长的正负而变化 导入java.util.Iterator; 导入java.util.NoSuchElementException; 公共类递增器实现Iterable{ 专用整数电流; 私有整数步长; 私有整数启动; 私有整数端; 公共递增整数开始,整数结束{ this.start=start; this.end=end; 这个步骤=开始>结束?-1:1; this.current=null; } 按整数偏移量的公共递增器{ 这一步=偏移量; 如果偏移量<0&&this.start
返回值我认为赋值是自己编写Incrementer类。在中,将是一个静态函数,用于创建Incrementer的新实例。