使用for循环更新sum(例如spaces=spaces+2)

fv2wmkja  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(233)

好的,我正在做一个程序,可以画垂直线,水平线,对角线!我有点搞不清楚我的一个输出,没有任何意义。
所以我的psudocode是这样的:

//enter a char
  //enter a number that will determine how long the line is
  //define with easyreader what type of line it will be (hori, vert, diag)
  //the idea of making the diag lines was this...

  @
  (two spaces) @
  (four spaces) @
  (six spaces)   @

  //we could use the sum spaces = spaces + 2; to keep on calculating what 
  //the previous spaces was

代码是:

class starter {

    public static void main(String args[])
    {   
        System.out.print("What char would you like? ");
        EasyReader sym = new EasyReader();
        String chars = sym.readWord();
        System.out.print("How long would you like it to be? ");
        int nums = sym.readInt();
        System.out.print("Diag, Vert, or Hori? ");
        //you want to read the __ varible, not the sym.readX()
        String line = sym.readWord();

        System.out.println("");
        System.out.println("");

        if(line.equals("Hori")){
            for(int x = 0; x < nums; x++){
                System.out.print(chars + " ");  
            }
        }

        else if(line.equals("Vert")){
            for(int y = 0; y < nums; y++){
                System.out.println(chars + " ");
            }
        }

        else{
            for(int xy = 0; xy < nums; xy++){
                for(int spaces = 0; spaces < nums; spaces++){
                spaces = spaces + 2;
                System.out.print(spaces + " ");
                System.out.println(chars);
                }
            }
        }

    }
 }

在底部,您将看到一个名为xy的for循环,它将读取行的长度。在这个条件下,for循环将控制空间。但是,由于某些原因,总和没有正确更新。输出总是:

2 (char)
 5 (char)
 8 (char)
 2 (char)
 5 (char)
 8 (char)
 ...

输出应为:

2 (char)
 4 (char)
 8 (char)
 ...

编辑*********因为我现在需要帮助,所以这里是一个例子(所以我不必在评论中解释太多)
例如:如果用户把他想要的线多达5个单位。有两个for循环,一个控制他想要多少空格,一个控制打印出多少字符,输出将是2,4,6,8,10。

8ehkhllq

8ehkhllq1#

for 循环语句,你说“增加” spaces 每次迭代后的( spaces++ ):

for(int spaces = 0; spaces < nums; spaces++){

在循环体中,您还要求将其增加2:

spaces = spaces + 2;

所以每次迭代都会增加3。
顺便说一下,您的嵌套循环似乎有问题(如果我正确理解其意图的话)。如果外环 xy )在每次迭代中画一条线,那么应该为当前行输出缩进的内部循环必须由 xy (乘以2)而不是 nums . 我会这样写:

for (int xy = 0; xy < nums; xy++) {
    for (int spaces = 0; spaces < xy*2; spaces += 2) {
        System.out.print(" ");
    }
    System.out.println(chars);
}
oyt4ldly

oyt4ldly2#

其实问题就在这一部分:

for(int spaces = 0; spaces < nums; spaces++){
      spaces = spaces + 2;
      System.out.print(spaces + " ");
      System.out.println(chars);
}

当节目开始的时候 spaces = 0 然后这部分就要开始了 spaces =spaces + 2 现在 spaces 等于 2 ,所以我们有 spaces = 2 程序打印 2 ,之后 spaces 增量 1 使用 spaces++ 零件
现在 spaces 等于 3 ,这意味着 spaces=3 在那之后这条线就要开通了 spaces = spaces + 2 所以 spaces 变成 5 如果我们永远这样做,我们就会有这样的数字序列: 2 5 8 11 14 .... 事实上,这是因为我们在增加 spaces3 在每次迭代中
如果在此表单中修改代码,问题将得到解决:

for (int xy = 0; xy < nums; xy++) {
    for(int spaces = 0; spaces < xy*2; spaces += 2)
          System.out.print(spaces + " ");
    System.out.println(chars);
}
dauxcl2d

dauxcl2d3#

因为你每次都要加3个空格

for(int spaces = 0; spaces < nums; spaces++){
    spaces = spaces + 2;

空格++空格+=1
空格=空格+2;空格+=2

相关问题