簡體   English   中英

如何改變循環方向

[英]How to change loop direction

我有 3x3 星形立方體,但我讓它可以根據我們的輸入繼續打印,它的循環會下降,而不是向右,如何更改為向右循環

import java.util.scanner;

Public class program {
    Public static void main(String [] args) {
        Scanner getInp;
        int a;
        
        getInp = new Scanner(System.in);
        a = getInp.nextInt();

        for (int x=0; x<a; x++) {
            for (int y=0; y<3; y++) {
                for (int z=0; z<3; z++) {
                    System.out.print("* ");
                }
                System.out.println("\n");  
            }
        }
    }
}

我猜你想打印一些 3x3 的立方體,比如

* * *.   * * *.  * * *
* * *.   * * *.  * * *
* * *.   * * *   * * *

如果是這種情況,那么您必須更改第二個內部循環之后的行,即 y 一個。 因為您只能逐行打印。

Scanner getInp;
        int a;
        
        getInp = new Scanner(System.in);
        a = getInp.nextInt();

        for (int x=0; x<a; x++) {
            for (int y=0; y<3; y++) {
                for (int z=0; z<3; z++) {
                    System.out.print("* ");
                }
                System.out.print(" ");
            }
System.out.println("\n"):
        }

如果是這樣的話試試這個..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM