Jump to content

Recommended Posts

Posted

Hello, I have been struggling with this program for a while and was wondering if anyone could help.

 

I am trying to make a program where the user inputs an int 0-9 and then it will print out something like this:

 

if user input=1 if user input = 3 I hope you guys get what im trying to say. so far i have:

1 **1

*21

321

import java.util.Scanner;
public class Lab07 {
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
int x;
System.out.println("Enter a number 1-9: ");
x = stdIn.nextInt();
while (x <=0 || x>=10)
{
System.out.println("Please enter a number 1-9: ");
x = stdIn.nextInt();
}
int i,j,k;
for (i=1; i<= x;i++)
{
for (j=1;j<=x-i;j++)
{
System.out.print("*");
}
for (k=x-i;k<=9;k++)
{
System.out.print(k);
}
System.out.println();
}
stdIn.close();
}
}
THANKS!!!
Posted (edited)

Try something like:

 

for( j = 1; j <= x; j++ ) {

for( i = x; i >= 1; i-- )

{

if( i > j )

{

// print star

}

else

{

// print i

}

}

}

Edited by Sensei

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.