简体   繁体   中英

Why won't my second method run?

I programmed the two parts of this assignment separately and they functioned as intended. When I combined the parts the program will only run the first method call (drawBox) and ignores the second one (drawPattern). What have I done wrong?

import java.util.*;

public class Patterns {

  public static final Scanner CONSOLE = new Scanner(System.in); 

  public static void main (String[] args) {
    System.out.println("Project 1 written by CHRISTOPHER TERRANOVA");
    System.out.println();
    drawBox();
    drawPattern();

  }

  public static void drawBox() {

    //Scanner input to determine number of boxes
    System.out.print("Enter the number of boxes desired:\n");
    int numBoxes = CONSOLE.nextInt();

    // for loop for the top half of the boxes
    System.out.print ("+");
    for (int i = 1; i<=numBoxes; i++){
      System.out.print("------+");
    }
    System.out.println();    

    // for loop for the 1st half of the middle of the boxes
    System.out.print ("|");
    for (int i = 1; i<=numBoxes; i++){
      System.out.print("      |");
    }
    System.out.println();    

    // repeated for loop for the 2nd half of the middle of the boxes
    System.out.print ("|");
    for (int i = 1; i<=numBoxes; i++){
      System.out.print("      |");
    }
    System.out.println();

    // for loop for the bottom of the boxes
    System.out.print ("+");
    for (int i = 1; i<=numBoxes; i++){
      System.out.print("------+");
    }
    System.out.println();
  }

  public static void drawPattern() {

    //Scanner inputs to determine width and height of pattern
    System.out.print("Enter width of the pattern:\n");
    int width = CONSOLE.nextInt();
    System.out.print("Enter height of the pattern:\n");
    int height = CONSOLE.nextInt();  

    for(int i=1; i<=width; i++) {
        System.out.print(" __ ");
    }
    System.out.println();

    for(int j=1; j<=height; j++) {
        for(int i=1; i<=width; i++) {
            System.out.print("/ \\__");
        }
        System.out.println();

    for(int i=1; i<=width; i++) {
        System.out.print("\\__/ ");
    }
    System.out.println();
    }

  }
}

This is working perfectly. You just have to enter 3 int value. I ran it with three 10 and the result is like this:

Enter the number of boxes desired:10
+------+------+------+------+------+------+------+------+------+------+
|      |      |      |      |      |      |      |      |      |      |
|      |      |      |      |      |      |      |      |      |      |
+------+------+------+------+------+------+------+------+------+------+
Enter width of the pattern:10
Enter height of the pattern:10
 __  __  __  __  __  __  __  __  __  __ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 
/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__
\__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 

Isn't it supposed to work like this?

Both ran through fine on my machine only crashing when I input non-integer values. This I assume is the expected result:

Connected to the target VM, address: '127.0.0.1:41443', transport: 'socket'
Project 1 written by CHRISTOPHER TERRANOVA

Enter the number of boxes desired:
3
+------+------+------+
|      |      |      |
|      |      |      |
+------+------+------+
Enter width of the pattern:
3
Enter height of the pattern:
3
Disconnected from the target VM, address: '127.0.0.1:41443', transport: 'socket'
 __  __  __ 
/ \__/ \__/ \__
\__/ \__/ \__/ 
/ \__/ \__/ \__
\__/ \__/ \__/ 
/ \__/ \__/ \__
\__/ \__/ \__/ 

Process finished with exit code 0

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