- public class LeftStarTriangle {
- public static void main(String[] args) {
- int row = 10;
- for (int i = 1; i <= row ; i++) {
- for (int j = 1; j <= row - i; j++) {
- System.out.print(" ");
- }
- for (int k = 1; k <= i; k++) {
- System.out.print("*");
- }
- System.out.println("");
- }
- }
- }
It is good to know and understand that in Java, for us to be able to print out something there is a functionality that is behind that which enables us to print whatsoever thing. It is called System.out.print(). out. print method prints the argument that is passed into it.
If I passed"Hello world" as an argument. then, it prints out "Hello World". we won't just be doing only that, we would do justice to loops too. there are different types of loops in java. Namely, For loops, while loops, and do-while loops. Printing of patterns requires iteration and this would make us utilize loops. without a loop(s), it can't be easier. I shall be explaining loops and how it works shortly.
Let us quickly do an overview of the System.out.print:
System – is a final class in java.lang package
out – is a static member of the System class and is the object of PrintStream
println()- is a method of PrintStream that prints whatever is passed to it on the standard output or your screen. To print we need to call println() method, but we can't call this method directly, we need the object of the class to which this method belongs. Hence why we should call it out.print().
There are different ways you can print apart from the introduced one above such as printf(), println(). They print as well too but in a way different from the one initially introduced. printf() is called a formatted print. so, you format it to your taste while println() prints on a new line.
The difference between print and println is that it is okay to use either print or println on the first line, but if you want your statement on a new line, you have to call on println() method, or else it stays on the same line.
The Printf
This kind of print makes you use any kind of format you want, using a format specifier.
The specifiers are %s, %d, %f. which represent String, digit, and float or double respectively.
Let me show you an example. Assuming I want to print" Hi 5".
- String print = "Hi";
- int num = 5;
- System.out.printf("%s%2d",print,num);
The above lines of codes print Hi 5.
Note: The 2 before d provides 2 spaces between the Hi and 5
We can print a Java pattern program in different designs. To learn the pattern program, we must have a deep knowledge of the Java loop, such as for loop, while loop, do-while loop as I had said above In this section, we will learn how to print a pattern in Java.
Fig 1.0 A left-angled star pattern.
Before writing the codes. Lets do Some overviews on each loop, starting with for loop.
For loop
- for(int i=1;i<=rows;i++){
- ........................
- }
- int 1 = 0;
- while(i <= 5){
- ..........
- i++;
- }
- do {
- // code block to be executed
- }
- while (condition);
INFINITE LOOP
Infinite loop as the name implies is the situation whereby the condition never becomes false. The loop keeps running endlessly.
I remember learning coding using these kind of exercises, thanks for memory
ReplyDelete