Skip to main content

New to Java help with conditionals

1 reply [Last post]
msuppelsa
Offline
Joined: 2012-10-28
Points: 0

Hello,
I'm new to the forum and have just begun learning Java. I am trying to get an output of:
*
**
***
****
*****
****
***
**
*
where the maximum stars is equal to int n. I know how to do this with a set number, but not if the maximum number printed is a variable entered by the user.

I hope I have posted this in the right place.
Many thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
shingnpr
Offline
Joined: 2012-09-21
Points: 0

package net.help;
public class Pyramid {
public static void main(String[] args) {
//here you can take the input from user and assign it to userInput variable.
//int userInput = Integer.parseInt(args[0]);
int userInput = 5;
for (int i = (userInput * 2 - 1); i > 0; i--) {
if (i >= userInput) {
for (int j = (userInput * 2 - 1); i <= j; j--) {
System.out.print("*");
}
} else {
for (int j = 0; i > j; j++) {
System.out.print("*");
}
}
System.out.println();
}
}
}

OUTPUT
*
**
***
****
*****
****
***
**
*
Hope this is helpful to you