Jump to content

Computer Science Help Java

Featured Replies

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!!!

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

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.