In Hình Tam Giác Ra Màn Hình Console Trong Java - Deft Blog
Có thể bạn quan tâm
Cho một số N, nhiệm vụ của chúng ta là in ra màn hình console như sau
N : 10 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * N :5 Output : * * * * * * * * * * * * * * *Chúng ta cần sử dụng các vòng lặp lồng nhau để in ra hình tam giác như mẫu trên. Vòng lặp bên ngoài được sử dụng để in cho số hàng tương ứng với N nhập vào. Vòng lặp bên trong đầu tiên được dùng để in các khoảng trắng trước dấu sao. Như bạn có thể thấy số lượng khoảng trắng giảm dần theo mỗi hàng trong khi chúng ta di chuyển về phía đáy của tam giác, vì vậy vòng lặp này chạy ít hơn một lần với mỗi lần lặp.
import java.util.*; // package to use Scanner class class pattern { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of rows to be printed"); int rows = sc.nextInt(); // loop to iterate for the given number of rows for (int i = 1; i <= rows; i++) { // loop to print the number of spaces before the star for (int j = rows; j >= i; j--) { System.out.print(" "); } // loop to print the number of stars in each row for (int j = 1; j <= i; j++) { System.out.print("* "); } // for new line after printing each row System.out.println(); } } }Nguồn
https://www.geeksforgeeks.org/printing-triangle-pattern-in-java/
Prev Article Next Article 0 0 votes Article Rating Subscribe Login Notify of new follow-up comments new replies to my comments

Bài viết liên quan
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More
In case of sale of your personal information, you may opt out by using the link Do Not Sell My Personal Information
Accept Decline Cookie Settings I consent to the use of following cookies: Necessary Marketing Analytics Preferences Unclassified Cookie Declaration About Cookies Necessary (0) Marketing (0) Analytics (0) Preferences (0) Unclassified (0) Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies. We do not use cookies of this type. Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers. We do not use cookies of this type. Analytics cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously. We do not use cookies of this type. Preference cookies enable a website to remember information that changes the way the website behaves or looks, like your preferred language or the region that you are in. We do not use cookies of this type. Unclassified cookies are cookies that we are in the process of classifying, together with the providers of individual cookies. We do not use cookies of this type. Cookies are small text files that can be used by websites to make a user's experience more efficient. The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies we need your permission. This site uses different types of cookies. Some cookies are placed by third party services that appear on our pages. Cookie SettingsDo you really wish to opt-out? Close
Cancel Confirm wpDiscuz0Would love your thoughts, please comment.x()x| ReplyInsertTừ khóa » Câu Lệnh In Ra Màn Hình Trong Java
-
Xuất 1 Số Ra Màn Hình Trong Java - Programming - Dạy Nhau Học
-
Java - Nhập Vào Thông Tin Của 1 Sinh Viên Và Hiển Thị Ra Màn Hình.
-
Java - In Ra 20 Số Nguyên Dương đầu Tiên Ra Màn Hình.
-
Tất Tần Tật Về Printf Trong Java - NIIT - ICT Hà Nội
-
Java Basic 1 : Câu Lệnh In Ra Màn Hình Console - YouTube
-
Nhập Xuất Trong Java - TEK4
-
Nhập Xuất Màn Hình Console Trong Java - KungFu Tech
-
Nhập Xuất Cho Người Mới Bắt đầu Bằng Ngôn Ngữ Lập Trình Java
-
Java Basic 1 : Câu Lệnh In Ra Màn Hình Console - Daotaobanhang
-
Học Nhanh Java Cho Người Mới Bắt đầu - Openplanning
-
System.intln Trong Java
-
Java Bài 9: Câu Lệnh Lặp - Yellow Code Books
-
Nhập Xuất Dữ Liệu Trong Java - Các Hàm Lớp Scanner
-
Bài 12: Câu Lệnh Break, Continue Trong Java - Lập Trình Java Cơ Bản