I want to do a java program with hollow in the middle and numbers decrease and increase at the sides based on user's input. Let say the user's input is 4, so the output should be like this:-
1 2 3 4
2 3
3 2
4 3 2 1
and i managed to do it until its like this:-
1 2 3 4
2
3
4 3 2 1
by using this code:
import java.util.*;
public class Exercise2 {
public static void main(String args[])
{
System.out.print("Enter an integer >0 and <21: ");
Scanner input = new Scanner (System.in);
int num = input.nextInt();
for (int i = 0; i < num; i++)
{
System.out.print((i+1) + " ");
}
System.out.println();
for (int j = 1; j < num-1; j++)
{
System.out.println((j+1) + " ");
}
// for (int j = num-1; j > 1; j--)
// {
// System.out.print(j + " ");
// }
for (int j = num; j > 0; j--)
{
System.out.print((j) + " ");
}
System.out.println();
if (num > 21 || num < 0)
{
System.out.println("Wrong number range");
}
}
}
I dont know how to do the other side of it to become 4 3 2 1 downwards. And i dont how how to insert blank space (System.out.print(" ")) so that the hollow is actually something printed, not just blank.
I would appreciate any help given. Thanks.
Aucun commentaire:
Enregistrer un commentaire