0
Scanner scanner = new Scanner(System.in);
        System.out.print("Masukkan nilai : ");
        int input = scanner.nextInt();
        int kolom = input -1;
            for(int i=1;i<input;i++){
                for(int j=input;j>i;j--){
                    System.out.print("*");
                }
                System.out.println();
            }

output of code is : output of code is :

but i want the output like : enter image description here

so i think i need get the first row and last column

how to do that

1
  • 1
    Adding some if statements will do the trick. Commented Sep 22, 2018 at 4:29

2 Answers 2

3

Please try this

Scanner scanner = new Scanner(System.in);
System.out.print("Masukkan nilai : ");
int index = scanner.nextInt();

for (int i = 0; i < index; i++) {
    System.out.print("*");
}
System.out.println();

for (int i = 1; i < index; i++) {
    for (int j = index; j > i; j--) {
        if (j == index || j == i + 1) {
            System.out.print("*");
        } else {
            System.out.print(" ");
        }
    }
    System.out.println();
}
Sign up to request clarification or add additional context in comments.

Comments

0
for(char c='*',i=0; i<input; c=' ',i++)
  for(int j=0; j<input-i; j++)
    System.out.print(j==0 ? "*" : j<input-i-1 ? c : "*\n");      

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.