Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to make a half arrow. This is what I have, but it won`t compile correctly

ID: 3848458 • Letter: T

Question

Trying to make a half arrow. This is what I have, but it won`t compile correctly. Any help will be super appreciated. Here is my code:

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;

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(int i=1; i <=5; i++) {

for(int j=1; j<=i; j++) // Draw arrow base (height = 3, width = 2) System.out.println("**");

System.out.println("**");

System.out.println("**");

for(int i=1; i<=2; i++) { for (int j=4; j>= i; j--) // Draw arrow head (width = 4)

System.out.println("****");

System.out.println("***");

System.out.println("**");

System.out.println("*");

}

}

}

}

The error I keep getting is this: DrawHalfArrow.java:30: error: variable i is already defined in method main(String[]) for(int i=1; i<=2; i++) ^ 1 error

Explanation / Answer

Corrected Code:

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;
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(int i=1; i <=5; i++) {
for(int j=1; j<=i; j++) // Draw arrow base (height = 3, width = 2) System.out.println("**");
System.out.println("**");
System.out.println("**");
for(i=1; i<=2; i++) { for (j=4; j>= i; j--) // Draw arrow head (width = 4)
System.out.println("****");
System.out.println("***");
System.out.println("**");
System.out.println("*");
}
}
}
}

PS: This code is working well. Previously there was an error as you have initialized i, j twice in the loops. If still facing problem with the code please let me know in the comment section.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote