Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 for在控制台中获得锯齿形输出?_Java_Arrays_For Loop_Infinite Loop - Fatal编程技术网

如何使用java for在控制台中获得锯齿形输出?

如何使用java for在控制台中获得锯齿形输出?,java,arrays,for-loop,infinite-loop,Java,Arrays,For Loop,Infinite Loop,我对编码非常陌生,但并不擅长: 我一直在努力为我的班级做一个项目,现在已经有好几个小时了,我在互联网上搜索,几乎没有什么用处 我需要让它出现在我的控制台上,就像一个球在来回反弹一样,球反弹的宽度由用户设置 到目前为止,我的输出只生成一个无限的从左到右的对角线,跨越用户选择的宽度 到目前为止,我的代码是: import java.util.Scanner; public class Midterm1_1 { public static void main(String[] args) {

我对编码非常陌生,但并不擅长:

我一直在努力为我的班级做一个项目,现在已经有好几个小时了,我在互联网上搜索,几乎没有什么用处

我需要让它出现在我的控制台上,就像一个球在来回反弹一样,球反弹的宽度由用户设置

到目前为止,我的输出只生成一个无限的从左到右的对角线,跨越用户选择的宽度

到目前为止,我的代码是:

import java.util.Scanner;

public class Midterm1_1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        double yourNum;
        System.out.println("How many spaces wide would you like the ball to bounce?");
        yourNum= scan.nextDouble();
        String ballBounce = new String ("o")
        int count = 0;
        while (count < 50){
            System.out.println(ballBounce);
            String x = " ";
            for (int row = 1; row < yourNum; row++) {
                System.out.println(x+"o");
                x+= " ";
            }
        }
    }
}   
如何让它从右向左返回?我的假设是另一种说法。但到目前为止,我所尝试的一切都不起作用


提前谢谢。

下面的代码不一定是最短的,它是为了让OP了解一些想法,但没有给她/他一个直接的答案。如果OP会照着原样复制代码而不进行修改,那么老师应该清楚代码是从某处复制来的。如果OP理解它,将很容易清洁和简化解决方案

下面有两种方法,只有在阅读代码并理解它之后,才能选择另一种方法。我故意排除了另一种方法:这是一种学习练习,所以最明显的一种方法留作练习

对于第一种方法,请阅读

对于第二种方法,请阅读

public class Midterm1_1 {
    static private String ballBounce = "o";


    public static void paintBallAt_1(int x) {
       if(x==0) {
           System.out.println("o");
       }
       else {
           // will get your format strings as "%1$1c\n", "%1$2c\n", etc
           String formatString="%1$"+x+"c\n";
           System.out.printf(formatString, 'o');
       }
    }

    // 6x20=120 spaces at max. Limit or expand them how you see fit
    static public final String filler=
        "                    "+
        "                    "+
        "                    "+
        "                    "+
        "                    "+
        "                    "
    ;
    static public final int MAX_BOUNCE=filler.length();

    public static void paintBallAt_2(int x) {
       if(x>MAX_BOUNCE) {
         x=MAX_BOUNCE;
       }
       if(x<0) {
          x=0;
       }
       String bounceSpace=filler.substring(0, x);
       System.out.println(bounceSpace+"o");
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        int yourNum;
        System.out.println("How many spaces wide would you like the ball to bounce?");
        yourNum= scan.nextInt();
        int count = 0;
        while (count < 50) {
            System.out.println(ballBounce);
            String x = " ";
            for (int row = 1; row < yourNum; row++) {
                System.out.println(x+"o");
                x+= " ";
            }

            // bounce it to the left
            for (int row = yourNum-1; row >= 0; row--) {
                switch(row % 2) {
                   case 0:
                        paintBallAt_1(row);
                        break;
                   default:
                        paintBallAt_2(row);
                        break;
                }
            }
        }
    }
}