简体   繁体   中英

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

I am currently taking an intro Java class to get my feet wet when it comes to programming. As one of my assignments, I was instructed to build a robot character using methods, parameters, and for loops.

I am having trouble getting my parameters to correctly loop how they are supposed to. I am not sure what I am doing wrong.

What I have:

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("._________.");
    }}

As you can see, I need my neck, rectanglebody, and legs methods to repeat as many times as I have set in the parameters.

When I run this, I only get the print lines but no loops:

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

Any help would be appreciated!

You passed the arguments correctly to your rectangleBody(int i, int j) method but then in the method definition, you have not used the for loops correctly. Both your for loops are doing nothing because you terminated them by putting a semicolon next to them. Instead nest the for loops (one inside another) and print the # Try this code:

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("._________.");
    }
}

Inside the rectangleBody method, your index variables should be different; the first loop uses an 'i' while the second uses both 'j' and 'i'. Also, your loops are closed with a semi colon: give them a body by adding braces '{ }' Try this:

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

You'll also need to add forloops inside your methods legs, neck, etc. to repeat the System.out.print command.

Your for loop is incorrect for your rectangleBody method. First, you are using i twice instead of i in one loop and j in the next. Secondly, you shouldn't need to pass in parameters. Your loops will stop when i=9 and j=10 respectively.

This code should give you a good start:

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 method should be like this:

// 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
    }
}

Try this one:

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. If you don't use any braces {} for your for then only the next statement is looped. You have to remove the semicolons after the for because you are currently looping an empty statement ( ; ). To avoid troubles I recommend to be painstakingly accurate with the line indentation.

  2. No mistake, but if you just want to do something X times, then I would write it like this: for (int i = 0; i < X; i++) because this is how you will loop arrays and what your eye should get used to see quickly, in Java most things have a 0-based index.

  3. The String always starts printing on the left. It needs padding (spaces in front) and maybe you even want to restrict it to only odd numbers, so it doesn't look ugly.

  4. println() adds a newline character \\n , so you always only print one character, there is also a print() method. But because you have well structured data you don't need to print the characters one by one.

If you put this all together you end up with something like this:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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