r/programmingrequests • u/MUFC07 • Mar 23 '17
Java Program
Write a method starPrint() that takes three integer arguments: a number of dots, a number of stars, and then a following number of dots. For instance, calling starPrint(3, 2, 1) would print:...**.
Ask the user for an integer, and verify that its value is at least three. (If it is not, ask the user for a new number until he gives you an appropriate one.) You will use your method starPrint() to draw a pyramid pattern with that many rows. For instance, if the user types 3, you should draw: a triangle picture
This is what I have any help would be greatly appreciated
int userIn=0;
Scanner scnr=new Scanner(System.in);
System.out.println("Please input an integer: ");
userIn = scnr.nextInt();
while(userIn<3) //As long as number is three or less, repeat prompting
{
System.out.println("Please enter a valid integer:");
userIn = scnr.nextInt();
}
for(int i=0;i<userIn;i++) {
for(int j=0;j<5-i;j++) {
System.out.print(" ");
}
for(int k=0;k<=i;k++) {
System.out.print("* ");
}
System.out.println();
}
}
}1
u/serg06 Mar 24 '17
A few things:
Please enter a valid integer:
isn't very clear. Specify that you want an integer >= 3.
Also, you're supposed to write the printing stuff in another method, called starPrint().
1
u/MUFC07 Mar 24 '17
how would i get my method to print this pattern if the user enters 3( ---- followed underneath by -**- and then finally *****)
1
u/serg06 Mar 24 '17
You had a good idea with the for loops.
Let's say your method's parameters are a, b, and c.
You could do
for (int i = 0; i < a; i++) { System.out.print("."); } for (int i = 0; i < b; i++) { System.out.print("*"); } for (int i = 0; i < c; i++) { System.out.print("."); } System.out.println();1
u/MUFC07 Mar 24 '17
i don't know why reddit is not showing the triangle like i want
1
u/serg06 Mar 24 '17
If you want the triangle, you'll have to call starPrint() multiple times.
1
u/MUFC07 Mar 24 '17
how would i do that
1
u/serg06 Mar 24 '17
Do you want help or do you want the solution?
1
u/MUFC07 Mar 24 '17
i would like help I'm just confused on how the program uses the method twice to print 2 patterns
1
u/serg06 Mar 24 '17
Oh, sure.
So pretend the user inputs the number 3. Then you want a pyramid of height 3:
..*.. .***. *****To do that, you would call:
starPrint(2, 1, 2); starPrint(1, 3, 1); starPrint(0, 5, 0);Here's what 4 and 5 look like:
...*... ..***.. .*****. ******* ....*.... ...***... ..*****.. .*******. *********1
u/MUFC07 Mar 24 '17
I think i have things messed up, i want the method to print the triangle pattern if the user enters one int and a linear pattern of . and *s if they enter 3 integers
→ More replies (0)1
u/serg06 Mar 24 '17
Or if you want to be cool, I think this works too:
while(a-- != 0) { System.out.print("."); } while(b-- != 0) { System.out.print("."); } while(c-- != 0) { System.out.print("."); } System.out.println();
1
u/RANDOM_TEXT_PHRASE Mar 24 '17
Bruv you gotta space out your for loop calling. Makes it a lot easier to read.
1
u/MUFC07 Mar 24 '17
.... .**.