【问题标题】:Java and programming newbie. What am I doing wrong? (Method, Parameters)Java和编程新手。我究竟做错了什么? (方法、参数)
【发布时间】:2015-04-03 10:25:15
【问题描述】:

我目前正在学习 Java 入门课程,以了解编程方面的情况。作为我的任务之一,我被指示使用方法、参数和 for 循环来构建机器人角色。

我无法让我的参数正确循环它们应有的方式。我不确定我做错了什么。

我有什么:

public class Assignment3 {

    public static void main(String[] args) {
    head();
    neck(2);
    rectangleBody(9,10);
    legs(6);
    feet(); 
}

    public static void feet() {
        System.out.println("=====   =====");

    }

    public static void legs(int i) {
        System.out.println(" | |    | |");

    }

    public static void rectangleBody(int i, int j) {
        for (i=1;i<=9;i++);
        for (j=1;i<=10;i++);
        System.out.println("#");


    }

    public static void neck(int i) {
        System.out.println("    | |");
    }

    public static void head() {
        System.out.println(".---------.");
        System.out.println("|   O O   |");
        System.out.println("|    <    |");
        System.out.println("|   ---   |");
        System.out.println("._________.");
    }}

如您所见,我需要我的颈部、矩形体和腿部方法重复我在参数中设置的次数。

当我运行它时,我只得到打印行但没有循环:

.---------.
|   O O   |
|    <    |
|   ---   |
._________.
    | |
#
 | |    | |
=====   =====

任何帮助将不胜感激!

【问题讨论】:

    标签: java methods parameters


    【解决方案1】:

    您将参数正确传递给您的 rectangleBody(int i, int j) 方法,但是在方法定义中,您没有正确使用 for 循环。你的两个 for 循环都没有做任何事情,因为你通过在它们旁边放一个分号来终止它们。而是嵌套 for 循环(一个在另一个循环中)并打印 # 试试这个代码:

    public class Assignment3 {
    
    public static void main(String[] args) {
        head();
        neck(2);
        rectangleBody(9,10);
        legs(6);
        feet(); 
    }
    
        public static void feet() {
            System.out.println("=====   =====");
    
        }
    
        public static void legs(int i) {
            System.out.println(" | |    | |");
    
        }
    
        public static void rectangleBody(int i, int j) {
            for (i=1;i<=9;i++)
            {
               for (j=1;i<=10;i++)
               {
                  System.out.println("#############"); //Nest the for loop and then print #
               }
            }
    
        }
    
        public static void neck(int i) {
            System.out.println("    | |");
        }
    
        public static void head() {
            System.out.println(".---------.");
            System.out.println("|   O O   |");
            System.out.println("|    <    |");
            System.out.println("|   ---   |");
            System.out.println("._________.");
        }
    }
    

    【讨论】:

      【解决方案2】:

      在 rectangleBody 方法中,你的索引变量应该是不同的;第一个循环使用“i”,而第二个循环使用“j”和“i”。此外,您的循环用分号关闭:通过添加大括号 '{ }' 给它们一个主体 试试这个:

      for (i = 1; i <= 9; i++) {
           for (j = 1; j <= 10; j++) {
                  System.out.println("#");
            }
      }
      

      您还需要在方法的腿、脖子等中添加 forloop 以重复 System.out.print 命令。

      【讨论】:

        【解决方案3】:

        您的 for loop 不适合您的 rectangleBody 方法。首先,您在一个循环中使用了两次i,而不是i,在下一个循环中使用了j。其次,您不需要传入参数。您的循环将分别在 i=9j=10 时停止。

        这段代码应该会给你一个好的开始:

        public static void rectangleBody() {
            for (i = 1; i <= 9; i++) {
                System.out.print("#");
            }
            for (j = 1; j <= 10; j++) {
                System.out.println("#");
            }
        }
        

        【讨论】:

        • 哇,非常感谢大家!我整天都在为此苦苦挣扎。疯狂我的问题最终变得相当简单
        【解决方案4】:
        public static void rectangleBody(int i, int j) {
            for (i=1;i<=9;i++);
            for (j=1;i<=10;i++);
            System.out.println("#");
        }
        

        这个方法应该是这样的:

        // this line called 'comment'. text starting with '//' is for humans, not computers!
        public static void rectangleBody(int width, int height) { // names should be explaining what it does
            for (int i=0; i<height; i++) { // create variable 'i' only used within foor loop
                for (int j=1; j<width; j++) { // use j here, not i!
                    System.out.print("#"); // 'print' method don't change line after print
                }
                System.out.println(); // change line after one line of body drew
            }
        }
        

        【讨论】:

          【解决方案5】:

          试试这个:

          public static void rectangleBody(int i, int j) {
              for (int x=1; x<=i; x++) {
                  for (int y=1; y<=j; y++) {
                      System.out.print("#");
                  }
                  System.out.println("");
              }
          }
          

          【讨论】:

            【解决方案6】:
            public static void rectangleBody(int i, int j) {
                for (i=1;i<=9;i++);
                for (j=1;i<=10;i++);
                System.out.println("#");
            }
            
            1. 如果你的for 不使用任何大括号{},那么只有下一条语句被循环。您必须删除 for 之后的分号,因为您当前正在循环一个空语句 (;)。为避免麻烦,我建议对行缩进非常准确。

            2. 没错,但如果你只是想做 X 次,那么我会这样写:for (int i = 0; i &lt; X; i++) 因为这是循环数组的方式,也是你的眼睛应该习惯快速看到的内容,在 Java 中,大多数事物都有一个从 0 开始的索引。

            3. 字符串总是从左边开始打印。它需要填充(前面有空格),也许你甚至想把它限制为只有奇数,所以它看起来并不难看。

            4. println() 添加一个换行符\n,所以你总是只打印一个字符,还有一个print() 方法。但是由于您拥有结构良好的数据,您不需要逐个打印字符。

            如果你把这一切放在一起,你最终会得到这样的结果:

            public static final int headWidth = 11; // constant
            
            public static final rectangleBody(int x, int y) {
            
              // fix x if out of range or even
              if (x > headWidth)
                x = headWidth;
              else if (x < 1)
                x = 1;
              else if (x % 2 == 0) // % = modulo ==> it is an even number
                x--;
            
              // calculate padding
              int pad = (headWidth  - x) / 2; // divide by 2, because only need to pad left
            
              // create a line of the body
              String line = "";
              for (int i = 0; i < pad; i++)
                line += " ";
              for (int i = 0; i < x; i++)
                line += "#";
            
              //print y lines
              for (int i = 0; i < y; i++)
                System.out.println(line);
            }
            

            【讨论】:

              猜你喜欢
              • 2013-08-06
              • 1970-01-01
              • 1970-01-01
              • 2016-07-18
              • 2021-04-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-12-23
              相关资源
              最近更新 更多