samedi 1 avril 2017

[Java]Program not outputting in correct format -- drawing an arrow with asterisks based on user input

So I'm trying to understand why this code isn't outputting in the correct asterisk arrow format that is asked of me. Here's the prompt for this program:

This program outputs a downwards facing arrow composed of a rectangle and a right triangle. The arrow dimensions are defined by user specified arrow base height, arrow base width, and arrow head width.

(1) Modify the given program to use a loop to output an arrow base of height arrowBaseHeight.

(2) Modify the given program to use a loop to output an arrow base of width arrowBaseWidth. Use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the height of the arrow base.

(3) Modify the given program to use a loop to output an arrow head of width arrowHeadWidth. Use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the height of the arrow head.

(4) Modify the given program to only accept an arrow head width that is larger than the arrow base width. Use a loop to continue prompting the user for an arrow head width until the value is larger than the arrow base width.

Example output for arrowBaseHeight = 5, arrowBaseWidth = 2, and arrowHeadWidth = 4:

Enter arrow base height: 5
Enter arrow base width: 2
Enter arrow head width: 4

**
**
**
**
**
****
***
**
*

If you have any suggestion as to how I could be going about this in a better way, then that would be greatly appreciated. I included the for for if else for for statements as a precaution to try to stop user input for the arrow head width if it's less than or equal to the arrow base width, and this is basically where my problem lies. If you notice that I've done the rest or any else of the program wrong, then please tell me, as I'd like to learn how to go about this the correct way.

import java.util.Scanner;

public class DrawHalfArrow {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int arrowBaseHeight = 0;
      int arrowBaseWidth  = 0;
      int arrowHeadWidth = 0;
      int j;
      int i;

  System.out.println("Enter arrow base height: ");
  arrowBaseHeight = scnr.nextInt();

  System.out.println("Enter arrow base width: ");
  arrowBaseWidth = scnr.nextInt();

  System.out.println("Enter arrow head width: ");
  arrowHeadWidth = scnr.nextInt();


  for (i = 0; i < arrowHeadWidth; i++) {
  for (j = arrowHeadWidth; j>i; j--) {
  }

      if (arrowHeadWidth <= arrowBaseWidth) {

         System.out.println("Please enter an arrow head width that is greater than the arrow base width.");
         arrowHeadWidth = scnr.nextInt();
      }else {
          for ( i = 0; i < arrowBaseHeight; i++) {
          for ( j = 0; j < arrowBaseWidth; j++) {
          System.out.print("*");
          }
          System.out.println();
          }
         System.out.print("*");
      }
  }
  System.out.println();

  return;
  }
}

Aucun commentaire:

Enregistrer un commentaire