繁体   English   中英

Java和编程新手。我究竟做错了什么? (方法,参数)

[英]Java and programming newbie. What am I doing wrong? (Method, Parameters)

我目前正在参加一个简介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   |
|    <    |
|   ---   |
._________.
    | |
#
 | |    | |
=====   =====

任何帮助,将不胜感激!

您正确地将参数传递给了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("._________.");
    }
}

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

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

您还需要在方法腿,颈部等中添加forloops以重复System.out.print命令。

您的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("#");
    }
}
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
    }
}

试试这个:

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("");
    }
}
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之后删除分号for因为您当前正在循环一个空语句( ; )。 为了避免麻烦,我建议使用线压痕精确准确。

  2. 没错,但是如果你只是想做X次的事情,那么我会这样写: for (int i = 0; i < 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM