所以,我需要得到这个输出:
循环1输出(低= 1,高= 10,增量= 1)
1 2 3 4 五 6 7 8 9 10
和
循环1输出(低= 2,高= 9,增量= 3)
2 五 8
但我只是第二个。 这是我的代码:
public void loop1(int lower, int upper, int increment)
{
System.out.println("Loop 1 Output (lower= " + lower+ "," +
" upper= " + upper+ "," + " increment= " + increment+ ")");
for(int i = lower; i <= upper; i += increment)
{
System.out.println(i);
}
System.out.println("-----------------------------");
}
你能帮帮我吗?
更新: 这是我的代码:
public void loop1(int lb, int ub, int incr)
{
System.out.println("Loop 1 Output (lb = " + lb + "," +
" ub = " + ub + "," + " incr = " + incr + ")");
for(int i = lb; i <= ub; i += incr)
{
System.out.println(i);
}
System.out.println("-----------------------------");
}
public void loop2(int lb, int ub, int incr)
{
System.out.println("Loop 2 Output (lb = " + lb + "," +
" ub = " + ub + "," + " incr = " + incr + ")");
int n = lb;
while(n <= ub)
{
System.out.println(n);
n += incr;
}
System.out.println("-----------------------------");
}
这是我的输入类:
public class Driver
public static void main (String [] args)
Loop lp = new Loop();
lp.loop1(1, 10, 1);
lp.loop2(1, 10, 1);
lp.loop1(2, 9, 3);
lp.loop2(2, 9, 3);
谢谢!
答案 0 :(得分:0)
答案 1 :(得分:0)
使用loop1
和1, 10, 1
2, 9, 3
public static void main(String[] args) {
loop1(1, 10, 1);
loop1(2, 9, 3);
}
然后我相信您需要static
方法,并且可以使用printf()
格式化输出,例如
public static void loop1(int lower, int upper, int increment) {
System.out.printf("Loop 1 Output (lower=%d, upper=%d, increment=%d)%n",
lower, upper, increment);
System.out.print(lower);
for (int i = lower + increment; i <= upper; i += increment) {
System.out.printf(" %d", i);
}
System.out.printf("%n-----------------------------%n");
}
无论如何,上述内容对我有用。