Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编写一个按顺序计数的程序,而不使用任何迭代循环_Java_Iteration - Fatal编程技术网

Java 编写一个按顺序计数的程序,而不使用任何迭代循环

Java 编写一个按顺序计数的程序,而不使用任何迭代循环,java,iteration,Java,Iteration,编写一个程序,在给定起始值和结束值时按顺序计数-不使用任何迭代编程循环,即while、for、do、for each等 您可以假设开始值和结束值始终为正值,并且开始值始终小于结束值。T 这里应该只有一个具有以下签名的方法: void countUp(int start, int end) {} 下面是start=0和end=5的输出示例: 0 1 2 3 4 5 使用递归,仅在以下行中使用: void countUp(int start, int end) { //Recursive

编写一个程序,在给定起始值和结束值时按顺序计数-不使用任何迭代编程循环,即while、for、do、for each等

您可以假设开始值和结束值始终为正值,并且开始值始终小于结束值。T

这里应该只有一个具有以下签名的方法:

void countUp(int start, int end) {}
下面是
start=0
end=5
的输出示例:

0
1
2
3
4
5

使用递归,仅在以下行中使用:

void countUp(int start, int end) {
    //Recursive Case 
    if(start <= end) {
        start += 1;
        countUp(start,end);
        //do some operations
    }
    //Base Case 
    else {
        //we have reached the end 
    }
} 
void倒计时(整数开始,整数结束){
//递归案例
如果(开始<代码>无效倒计时)(整数开始,整数结束)
{
系统输出打印项次(开始);
如果(开始<结束)
{
倒计时(开始+1,结束);
}
}

这应该行得通,

尝试使用java.util.BitSet

public void countUp(int start, int end){
    BitSet bitSet = new BitSet();
    bitSet.set(start, end + 1);
    System.out.println(bitSet);
}

我假设这是一个练习,让您能够使用递归,而不是乱搞
java.util.BitSet
,因此您可以使用下面的内容:

public class RecursionExample {
    public static void main(String[] args) {
        int start = 0;
        int end = 5;
        countUp(start, end); //calls your class countUp
    }

    public static void countUp(int start, int end) {
        System.out.println(start); //prints out start

        if (start < end){ //will run if start is less than end
            countUp(start+1, end); //calls countUp recurses it with start set to start + 1
        }

    }
}
公共类递归示例{
公共静态void main(字符串[]args){
int start=0;
int-end=5;
倒计时(开始,结束);//调用类倒计时
}
公共静态无效倒计时(整数开始,整数结束){
System.out.println(start);//打印出start
如果(开始<结束){//将在开始小于结束时运行
countUp(start+1,end);//calls countUp在start设置为start+1时对其进行递归
}
}
}

希望这有帮助!

我们不是来帮你做作业的。标记这种只发布作业相关问题的用户有效吗?我会说标记问题,如果他们坚持,最终会得到“lol,banzor”-ed。
public class RecursionExample {
    public static void main(String[] args) {
        int start = 0;
        int end = 5;
        countUp(start, end); //calls your class countUp
    }

    public static void countUp(int start, int end) {
        System.out.println(start); //prints out start

        if (start < end){ //will run if start is less than end
            countUp(start+1, end); //calls countUp recurses it with start set to start + 1
        }

    }
}