Kali ini saya akan membagikan cara membuat Persegi Dengan Bintang pada pemrograman java, .
dan kalian dapat membuat berbagai macam pola dengan cara mengedit syintaxnya, ..
silahkan dicoba dan di modifikasi sendiri, . . setelat slash itu comment, tidak termasuk syintax, . .
import java.util.Scanner;
public class SquareWithStars {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
System.out.print("Masukkan n : ");
int n = s.nextInt();
printPattern(n);
}
static public void printPattern(int size) {
for (int i = 0; i < size; i++) { // Create a loop for every line of the box
if (i == 0) // if it is the first line we want to print size number of *
for (int h = 0; h < size; h++)
if (h != size - 1)// prints it all the way to the last one
System.out.print(h + 1 + " ");
else System.out.println(size);// uses println for last to to start the next one off on a new line
else if (i == size - 1)// If it is the very last row then it prints size number of * using the for loop below
for(int k = (size - 1) * 3 + 1; k > (size - 1) * 2; k--)
System.out.print(k + " ");
else { // if it is neither first or last line it will print 1 asterisks spaces then asterisk
for(int x = 0; x < size; x++) // keeps track of the column that it is in
if(x == 0) // first column prints a * with print
System.out.print(((size - 1) * 4 - i) + 1);
else if(x == size - 1) // last one uses println for the last one to set it to a new line
System.out.println(" " + (size + i));
else System.out.print(" * "); // if it is not the first column or the last will print spaces
}
}
}
}
Semoga bermanfaat, . .
0 Response to "Membuat Persegi Dengan Bintang"
Post a Comment