I made a program that I made when I was a newcomer, now in my second year. For statement again. This is a continuation of Previous article.
Conclusion: I managed to complete it with the help of science synchronization
Please enter the height of the triangle
5
*
**
***
****
*****
****
***
**
*
I made it as follows.
Check32.java
package practice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Check32 {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the height of the triangle");
String line;
try {
line = reader.readLine();
int hight = Integer.parseInt(line);
for( int i = 0; i < hight; i++) {
for (int j = 0; j < i ;j++) {
System.out.print("*");
}
System.out.println();
}
for( int i = hight; i > 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
} catch (NumberFormatException e) {
System.out.println("Please enter an integer");
} catch (IOException e) {
e.printStackTrace();
}
}
}
I couldn't think of "j <i". After that, when writing a for statement, for (initial setting; end condition </ b>; continuous processing) {} I wrote that, and sometimes it didn't work as I expected.
Recommended Posts