Created
February 24, 2023 06:32
-
-
Save phuoctamm/8a15e0a4af04203b45a77241af1b73dc to your computer and use it in GitHub Desktop.
cau1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Scanner; | |
public class Cau1 { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
// Nhập số phần tử của mảng, dùng vòng lặp while kiểm tra điều kiện, nếu nhập dưới hoặc bằng 3 sẽ chạy lại vòng lặp tới khi nào ta nhận được > 3 | |
int n = 0; | |
while (n <= 3) { | |
System.out.print("Nhap so phan tu cua mang (lon hon 3): "); | |
n = scanner.nextInt(); | |
} | |
// Nhập các phần tử của mảng | |
int[] arr = new int[n]; // tạo một mảng trống dạng int có độ dài là n | |
for (int i = 0; i < n; i++) { | |
// yêu cầu nhập từng phần tử một | |
System.out.print("Nhap phan tu thu " + (i + 1) + ": "); | |
arr[i] = scanner.nextInt(); // lưu vào mảng arr | |
} | |
// Hiển thị mảng vừa nhập lên màn hình | |
System.out.println("Mang vua nhap: " + Arrays.toString(arr)); | |
// Tìm giá trị lớn nhất trong mảng | |
int max = arr[0]; | |
for (int i = 1; i < n; i++) { | |
// lặp từng phần tử ở trong mảng, nếu lớn hơn hiện tại thì biến max sẽ có giá trị của phần từ đó | |
if (arr[i] > max) { | |
max = arr[i]; | |
} | |
} | |
System.out.println("Gia tri lon nhat trong mang: " + max); | |
// Sắp xếp mảng theo thứ tự tăng dần và hiển thị lên màn hình | |
Arrays.sort(arr); // dùng hàm Arrays.sort để sắp xếp từ bé đến lớn | |
System.out.println("Mang da sap xep: " + Arrays.toString(arr)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment