In Hình Tam Giác Ra Màn Hình Console Trong Java - Deft Blog

HomeJavaIn hình tam giác ra màn hình console trong Java Tags:Java Exercise

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 guest Label {} [+] Name* Email* guest Label {} [+] Name* Email* 0 Comments Inline Feedbacks View all comments Load More Comments Học lập trình online

Bài viết liên quan

  • Số hoàn hảo là gì? Kiểm tra số hoàn hảo bằng Java

  • In hình chữ nhật có kích thước MxN trong Java

  • In hình tam giác ra màn hình console trong Java

  • Giải phương trình bậc 2 sử dụng Java code

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 Settings

Do you really wish to opt-out? Close

Cancel Confirm wpDiscuz0Would love your thoughts, please comment.x()x| ReplyInsert

Từ khóa » Câu Lệnh In Ra Màn Hình Trong Java