【问题标题】:Simple Java Pyramid using nested for loops使用嵌套 for 循环的简单 Java 金字塔
【发布时间】:2013-10-01 01:11:02
【问题描述】:

我在尝试找出一种使用用户输入创建金字塔的方法时遇到了很多麻烦。这是它的样子。

Enter a number between 1 and 9: 4
O
O
O
O
OOOO
OOOO 
OOOO 
OOOO
O
OO
OOO
OOOO

这是我目前所拥有的

public static void main(String[] args) {
    int number;
    Scanner keyboard = new Scanner(System.in); 

    System.out.print("Enter a number between 1 and 9: ");
    number = keyboard.nextInt();

    for (int i = 1; i < 10; i++){
        for (int rows = number; number < i; rows++){
            System.out.print("O");
        }
        System.out.println();  
    }
}

我完全理解我想要完成的任务,但我并不完全理解 for 循环的工作原理。任何帮助将不胜感激,因为我完全迷失了!

【问题讨论】:

  • for 循环只要声明中的条件为真,就会执行其体内的代码。
  • @SotiriosDelimanolis 这正是whiledo while 循环的工作原理......
  • @nhgrif 引用..but I do not completely understand how for loops work 也许他还不知道while/do while。在这种情况下,for 循环应该更好,因为他需要 iterate
  • 我建议你先在脑子里做这个问题。如果您无法在脑海中解决这个问题,那么您将很难用代码解决它。使用纸一步一步地执行程序,然后尝试在代码中复制它。
  • @porfiriopartida For loops 虽然有初始化和更新语句......这可能是原始海报混淆的一部分。

标签: java for-loop


【解决方案1】:

基本上for 循环是这样工作的

// Take this example (and also try it)
for (int i = 0; i < 10; i++) 
{
   System.out.println(i);
}


// Explanation
for(int i = 0; // the value to start at  - in this example 0
    i < 10;    // the looping conditions - in this example if i is less than 10 continue 
               //         looping executing the code inclosed in the { }
               //         Once this condition is false, the loop will exit
    i++)       // the increment of each loop - in this exampleafter each execution of the
               //         code in the { } i is increments by one
{
   System.out.println(i);  // The code to execute
}

使用不同的起始值、条件和增量进行实验,试试这些:

for (int i = 5; i < 10; i++){System.out.println(i);}
for (int i = 5; i >  0; i--){System.out.println(i);}

【讨论】:

    【解决方案2】:

    将 for 循环替换为:

        for (int i = 0; i < number ; i++){
            System.out.println("O");
        }
        for (int i = 0; i < number ; i++){
            for (int j = 0; j < number ; j++){
                System.out.print("O");
            }
            System.out.println();
        }
        for (int i = 1; i <= number ; i++){
            for (int j = 0; j < i ; j++){
                System.out.print("O");
            }
            System.out.println();
        }
    

    输出(数字 = 6):

    O
    O
    O
    O
    O
    O
    OOOOOO
    OOOOOO
    OOOOOO
    OOOOOO
    OOOOOO
    OOOOOO
    O
    OO
    OOO
    OOOO
    OOOOO
    OOOOOO
    

    【讨论】:

    • 解释可能比实际代码更有帮助。我确信 OP 宁愿从他们的作业中学到更多东西,而不是如何复制粘贴代码。尽管您的解决方案是正确的,但我正在认真考虑投反对票。
    • @DavidWallace 当我不确定如何做某事时 - 我会查看代码示例,如果逻辑有点复杂(例如,桶/基数排序)我什至可以运行用调试器。就是说,如果您觉得这个答案没有用,请随时投反对票 - 我保证不会亲自接受;)
    • 非常感谢!我现在明白了很多!与实际代码相比,大卫华莱士建议的解释要差得多。最好的学习方法是看它,理解它,然后自己重​​新应用它。再次感谢!
    【解决方案3】:

    一些见解..

    for(INIT_A_VARIABLE*; CONDITION_TO_ITERATE; AN_OPERATION_AT_THE_END_OF_THE_ITERATION)
    
    • 您可以初始化多个变量或调用任何方法。在 java 中,您可以初始化或调用相同类型的变量/方法。

    这些是有效的代码语句:

     for (int i = 0; i <= 5; i++) // inits i as 0, increases i by 1 and stops if i<=5 is false.
     for (int i = 0, j=1, k=2; i <= 5; i++) //inits i as 0, j as 1 and k as 2.
     for (main(args),main(args); i <= 5; i++) //Calls main(args) twice before starting the for statement. IT IS NONSENSE, but you can do that kind of calls there.
     for (int i = 0; getRandomBoolean(); i++) //You can add methods to determine de condition or the after iteration statement.
    

    现在..关于你的作业..我会使用这样的东西:

    遍历行的限制并将空格和所需字符添加到其位置。位置将由您所在的行决定。

    for (int i = 0; i <= 5; i++) {
      for (int j = 5; j > i; j--) {
        System.out.print(' ');
      }
      for (int j = 0; j < i; j++) {
        System.out.print("O ");
      }
      System.out.println();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多