【问题标题】:Nested For-Loop Explanation嵌套 For 循环说明
【发布时间】:2021-11-25 07:53:08
【问题描述】:

用java编写的代码

我正在尝试在同一行打印用户输入的“*”数量。

这是我所拥有的:

if (((input / 2) + 1) == ir) {
    for (int ij = 1 ; ij <= input; ij++) {
        System.out.print("*");
    }
}

if 语句正在测试我们是否处于我要制作的形状(领结)的中点。

我觉得我的逻辑和代码是正确的,但输入为 5,

这一行看起来像这样:***

代替:*****

有人可以向我解释为什么会这样吗?

这里是完整的代码:

import java.util.Scanner;

public class BowTie {
  public static void main(String [] args) {
    Scanner scnr = new Scanner(System.in);
    int input = scnr.nextInt();

    int stars = 1;
    int spaces = input - 2;
    if ((input % 2 == 1) && (input >= 1)) {
        for (int ir = 1; ir <= input; ir++) {
            for (int ic = 1; ic <= stars; ic++) {
                System.out.print("*");
            }
            for (int ic = 1; ic <= spaces; ic++) {
                if (((input / 2) + 1) == ir) {
                    for (int ij = 1; ij <= input; ij++) {
                        System.out.print("*");
                    }
                } else {
                    System.out.print(" ");
                }
            }
            if (((input + 1) / 2) != ir) {
                for (int ic = 1; ic <= stars; ic++) {
                    System.out.print("*");
                }
            }
            if ((input / 2) < ir) {
                stars--;
                spaces += 2;
            } else {
                stars++;
                spaces -= 2;
            }
            System.out.println();
        }
    } else {
        return;
    }
    scnr.close();
  }
}

【问题讨论】:

  • 向我们展示整段代码;不是嵌套循环的 sn-p。一个Minimal Reproducible Example
  • @ElliottFrisch 现在完整的代码就在那里。
  • 太棒了。现在告诉我们您期望8 的输出是什么。因为这对我来说不合适。但我不确定。
  • @ElliottFrisch 代码不会为偶数或小于 1 的任何数字产生输出。我现在看到我不小心没有在此处复制该条件。

标签: java for-loop nested-loops


【解决方案1】:

这是因为您使用的检查。 简而言之,检查这个修改后的脚本:

import java.util.Scanner;

public class BowTie {
  public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int input = scnr.nextInt();

    int stars = 1;
    int spaces = input - 2;
    if ((input % 2 == 1) && (input >= 1)) {
        for (int ir = 1; ir <= input; ir++) {
          
            if (((input + 1) / 2) != ir) { // add this check
              for (int ic = 1; ic <= stars; ic++) {
                  System.out.print("*");
              }
            }
            // change this logic: ---
            if (((input + 1) / 2) == ir) {
              for(int ic = 1; ic <= input; ic++) {
                System.out.print("*");
              }
            } else {
              for (int ic = 1; ic <= spaces; ic++) {
                System.out.print(" ");
              }
            }
            // ---
            if (((input + 1) / 2) != ir) {
                for (int ic = 1; ic <= stars; ic++) {
                    System.out.print("*");
                }
            }
            if ((input / 2) < ir) {
                stars--;
                spaces += 2;
            } else {
                stars++;
                spaces -= 2;
            }
            System.out.println();
        }
    }
    scnr.close();
  }
}

(带有 cmets 的行是变化)

for (int ir = 1; ir &lt;= input; ir++) 中的第一个 for 正在打印您看到的星星。
此外,中间部分已更改为打印整行或空格(就像您所做的那样)。

【讨论】:

    【解决方案2】:

    您缺少if (((input + 1) / 2) != ir) 上的 else 条件,如下所示:

    import java.util.Scanner;
    
    public class BowTie {
      public static void main(String [] args) {
        Scanner scnr = new Scanner(System.in);
        int input = scnr.nextInt();
    
        int stars = 1;
        int spaces = input - 2;
        if ((input % 2 == 1) && (input >= 1)) {
            for (int ir = 1; ir <= input; ir++) {
                for (int ic = 1; ic <= stars; ic++) {
                    System.out.print("*");
                }
                for (int ic = 1; ic <= spaces; ic++) {
                    if (((input / 2) + 1) == ir) {
                        for (int ij = 1; ij <= input; ij++) {
                            System.out.print("*");
                        }
                    } else {
                        System.out.print(" ");
                    }
                }
                if (((input + 1) / 2) != ir) {
                    for (int ic = 1; ic <= stars; ic++) {
                        System.out.print("*");
                    }
                // Added else
                } else {
                    for (int ic = 1; ic <= (input - ir); ic++) {
                        System.out.print("*");
                    }
                }
                if ((input / 2) < ir) {
                    stars--;
                    spaces += 2;
                } else {
                    stars++;
                    spaces -= 2;
                }
                System.out.println();
            }
        } else {
            return;
        }
        scnr.close();
      }
    }
    
    

    不过,您的代码有点难以阅读和理解。您可能需要对其进行一些重构。

    【讨论】:

    • 添加该检查有效,但我不太清楚为什么。你能解释一下吗?我知道逻辑有点难以理解,但在这一点上,我只想让我的实验室上交并评分,哈哈。不过,我将来会牢记可读性。谢谢!
    • 问题是当你在领结的中间线上“工作”时,你错过了条件。这就是为什么你总是在该行中错过一些*
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多