Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops - Fatal编程技术网

Java 以倒置的直角形式打印奇数

Java 以倒置的直角形式打印奇数,java,loops,Java,Loops,我试着取一个数字并打印它是奇数,如下所示: if i take 5 as a number it should give this: 1 3 5 3 5 5 and if i take 9 it should do the same thing: 1 3 5 7 9 3 5 7 9 5 7 9 7 9 9 这就是我到目前为止所拥有的,我被困住了。我无法在3之后打印5,并在三角形中以5结尾: public class first{

我试着取一个数字并打印它是奇数,如下所示:

    if i take 5 as a number it should give this: 

    1 3 5
    3 5
    5

   and if i take 9 it should do the same thing:

  1 3 5 7 9
  3 5 7 9
  5 7 9
  7 9
  9
这就是我到目前为止所拥有的,我被困住了。我无法在3之后打印5,并在三角形中以5结尾:

public class first{
    static void afficher(int a){
    for(int i=1;i<=a;i++){
        if(i%2!=0){
            System.out.printf("%d",i);
        }
    }
    System.out.println();

    for(int j=3;j<=a-2;j++){
        if(j%2!=0){
            System.out.printf("%d",j);
        }
    }
}




     public static void main(String[]args){
        afficher(5);


    }

}

如果这样打印二维曲面,则该算法的时间复杂度为^2。因此,有两个嵌套的FOR:

通过不检查数字是否为奇数,而是采取步骤2,可以稍微优化算法


请参阅。

打印原因如下:

1 3 5 -> your i loop runs here (from 1 to 5)
3     -> your j loop runs here (from 3 to (less than OR equal to 5))
因此,我建议如下:

对通用值使用2个嵌套循环:

 i running from 1 to the input number increasing by 2
 j running from i to the input number increasing by 2 also ending with line change'/n'  
检查输入的数字是否为奇数


必须使用嵌套for循环来解决此问题。检查以下代码

public class OddNumberLoop {

 public static void main(String[] args) {

    Scanner inpupt = new Scanner(System.in);

    System.out.print("Input the starting number : ");
    int start = inpupt.nextInt();
    for(int i = 1 ; i <= start; i += 2){
        for(int x = i; x <= start; x += 2) System.out.print(x+ " ");
        System.out.println();
    }

 }
}

因为您正在打印曲面,所以在for循环中需要两个嵌套的for.a for循环?
 i running from 1 to the input number increasing by 2
 j running from i to the input number increasing by 2 also ending with line change'/n'  
public class OddNumberLoop {

 public static void main(String[] args) {

    Scanner inpupt = new Scanner(System.in);

    System.out.print("Input the starting number : ");
    int start = inpupt.nextInt();
    for(int i = 1 ; i <= start; i += 2){
        for(int x = i; x <= start; x += 2) System.out.print(x+ " ");
        System.out.println();
    }

 }
}