Java Basic- Ôn luyện về vòng lặp for, while, do-while trong java
Bài 1 :
Khai báo mảng số nguyên gồm 10 phần tử.
Yêu cầu :
- Nhập giá trị cho mảng đó
- Tính tổng giá trị các phần tử trong mảng và in ra màn hình
Bài 2 :
Khai báo mảng số nguyên gồm N phần tử (N nhập từ bàn phím)
Yêu cầu :
- Nhập dữ liệu cho mảng trên
- Tính tổng các phần tử chia hết cho 3 trong mảng và in ra màn hình kết quả.
Bài 3 :
In ra hình sau
*
**
***
****
*****
N = 5
Yêu cầu : Nhập N từ bàn phím, và in ra cây như hình trên
Bài 4 :
Cho biểu thức Fibonaci như sau
F(0) = 1
F(1) = 1
F(n) = F(n-1) + F(n-2)
Nhập từ bản phím số max. In ra tất cả các số Fibonaci có giá trị nhỏ hơn max.
Bài 5 :
Khai báo mảng gồm N số nguyên (N nhập từ bàn phím)
Sắp xếp mảng theo thứ tự tăng dần. và in mảng ra màn hình.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2021-01-22 10:07:10
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package onluyenvonglap;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class OnLuyenVongLap {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
//Bai 1:
/*int[] t = new int[10];
for(int i=0; i<10; i++){
System.out.println("Nhap phan tu thu: " +(i+1));
t[i]= sc.nextInt();
}
int sum=0;
for(int i =0;i<10;i++){
sum=sum+t[i];
}
System.out.println("Tong cac phan tu trong mang la: "+sum);*/
//Bai 2:
/*int n, sum;
System.out.println("Nhap so phan tu cua day: ");
n = sc.nextInt();
int[] t = new int[n];
for(int i=0; i<n; i++){
System.out.println("Nhap phan tu thu: "+(i+1));
t[i]=sc.nextInt();
}
sum=0;
for( int i=0; i<n; i++){
if(t[i]%3==0){
sum=sum+t[i];
}
}
System.out.println("Tong cac phan tu trong mang chia het cho 3 la: "+sum);*/
//Bai 3:
/*int n;
System.out.println("Nhap n = ");
n = sc.nextInt();
for(int i = 0; i<n; i++){
System.out.println("*");
for(int k=0;k<=i;k++)
System.out.print("*");
}*/
//Bai 4:
/*int n;
System.out.println("Nhap vao số max: ");
n = sc.nextInt();
System.out.print("Day so Fibonaci la: 1");
int F1=0;
int F2=1;
int Fn=1;
while(Fn<=n){
System.out.print(", "+Fn+" ");
F1 = F2;
F2 = Fn;
Fn = F1+F2;
}*/
//Bai 5:
int n;
System.out.println("Nhap vao so phan tu:");
n = sc.nextInt();
int[] t = new int[n];
for(int i=0; i<n; i++){
System.out.println("Nhap phan tu thu: "+(i+1));
t[i]=sc.nextInt();
}
int tam;
for(int i = 0; i < n - 1; i++){
for(int j = i + 1; j < n; j++){
if(t[i] > t[j]){
tam = t[i];
t[i] = t[j];
t[j] = tam;
}
}
}
for(int i=0; i<n;i++){
System.out.print(t[i]+", ");
}
}
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2021-01-22 10:00:45
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javalesson2;
import java.util.Scanner;
/**
*
* @author W10-2004
*/
public class ex06 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String s = "";
int n;
Scanner r = new Scanner(System.in);
n = r.nextInt();
for (int i = 0; i < n; i++) {
s += "* ";
System.out.println(s);
}
}
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2021-01-22 09:56:45
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javalesson2;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author W10-2004
*/
public class ex05 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<>();
Scanner r = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
a.add(r.nextInt());
}
int sum = 0;
for (int i = 0; i < 10; i++) {
if(a.get(i) % 3 == 0) sum += a.get(i);
}
System.out.println(sum);
}
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2021-01-22 09:55:08
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javalesson2;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author W10-2004
*/
public class ex04 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<>();
Scanner r = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
a.add(r.nextInt());
}
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += a.get(i);
}
System.out.println(sum);
}
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-01-22 09:51:13
Bai 5
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson2;
import java.util.Scanner;
/**
*
* @author MyPC
*/
public class Sapxep {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n;
n = scan.nextInt();
int[]a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scan.nextInt();
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if( a[i] > a[j] ){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-22 09:32:21
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2_bai_1;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int i;
int[] a = new int[10];
for( i =0 ;i< 10; i++){
Scanner scan = new Scanner(System.in);
System.out.format("Nhap so cho phan tu a[%d] =",i);
a[i] = scan.nextInt();
}
int tong = 0;
for( i =0 ;i< 10; i++){
tong = a[i] + tong;
}
System.out.println("Tong = "+tong);
}}
![NgNhan [APROTRAIN_ADF]](https://www.gravatar.com/avatar/8c2f7eb13764d610abe695cc366696a6.jpg?s=80&d=mm&r=g)
NgNhan
2020-06-04 04:47:35
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Bài 1:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter size of array: ");
int n = Integer.parseInt(scanner.nextLine());
int sum = 0;
int[] array = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("\nEnter element at position " + (i + 1) + ": ");
array[i] = Integer.parseInt(scanner.nextLine());
}
// Tính tổng của mảng
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
System.out.println("\nSum of array: " + sum);
sum = 0;
// Bài 2: Tổng phần tử chia hết cho 3
for (int i = 0; i < array.length; i++) {
if (array[i] % 3 == 0) {
sum += array[i];
}
}
System.out.println("\nSum of element divisible by 3: " + sum);
// Bài 3:
for (int i = 1; i < n + 1; i++) {
int j = 1;
while (j <= i) {
if (j == i) {
System.out.println("*");
} else {
System.out.print("*");
}
j++;
}
}
// Bài 5: Sắp xếp mảng
int tmp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (array[i] > array[j]) {
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
System.out.print("Array asc: ");
for (int i = 0; i < n; i++) {
System.out.print(array[i] + " ");
}
![Lê Xuân Dũng [JavaFree]](https://www.gravatar.com/avatar/af3e93d6e9bd94db12e2b8a1069aef68.jpg?s=80&d=mm&r=g)
Lê Xuân Dũng
2020-03-20 04:44:48
package in_hoa_thi;
import java.util.Scanner;
public class In_Hoa_thi {
public static void main(String[] args) {
int n;
System.out.println("Nhap n de in cay hoa thi: ");
Scanner sc = new Scanner(System.in);
n = Integer.parseInt(sc.nextLine());
int i,j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
![Lê Xuân Dũng [JavaFree]](https://www.gravatar.com/avatar/af3e93d6e9bd94db12e2b8a1069aef68.jpg?s=80&d=mm&r=g)
Lê Xuân Dũng
2020-03-19 13:52:19
Bài 5:
package sapxepmang_selectionsort;
import java.util.Scanner;
public class SapxepMang_SelectionSort {
private static void addArray(int[] A, int n) {
Scanner sc = new Scanner(System.in);
for(int i=0; i<n; i++) {
System.out.print("A["+i+"]: ");
A[i] = sc.nextInt();
}
}
private static void showArray(int[] A, int n) {
for(int i=0; i<n; i++) {
System.out.println("A["+i+"]: "+A[i]);
}
}
private static void Swap(int[] A, int a, int b) {
int temp = A[a];
A[a] = A[b];
A[b] = temp;
}
private static void SelectionSort(int[] A, int n) {
for(int i=0; i<n-1; i++) {
int minArr = i;
for(int j=i+1; j<n; j++) {
if(A[minArr]>A[j]) {
minArr = j;
}
}
if(i!=minArr)
Swap(A, minArr, i);
}
}
public static void main(String[] args) {
int[] A;
int n=0;
Scanner scn = new Scanner(System.in);
System.out.print("Nhập số phan tu cua mang: ");
n = scn.nextInt();
A = new int[n];
addArray(A, n);
System.out.println("Mảng trước khi sắp xếp (Selection Sort)");
showArray(A, n);
System.out.println("Mảng sau khi sắp xếp (Selection Sort)");
SelectionSort(A, n);
showArray(A, n);
}
}
![Phạm Kim Anh [JavaFree]](https://www.gravatar.com/avatar/2a2cc19e3234e41e7f8a049d835aaac7.jpg?s=80&d=mm&r=g)
Phạm Kim Anh
2020-03-19 13:11:21
package gokisoft.com;
import java.util.Scanner;
public class Bai2 {
public static int ChiaHetCho3 (int n) {
if(n % 3 == 0) {
return 1;
}
return 0;
}
public static void main(String[] args) {
int n;
int i;
Scanner sc = new Scanner(System.in);
System.out.println("Nhap do dai cua mang: ");
n=Integer.parseInt(sc.nextLine());
int []arr = new int [n];
for(i = 0 ; i < n; i++ ) {
System.out.println("Nhap phan tu thu "+ i +" : ");
arr[i] = Integer.parseInt(sc.nextLine());
}
double sum = 0;
for(i = 0 ; i < arr.length; i++ ) {
int check = ChiaHetCho3(arr[i]);
if(check == 1) {
sum += arr[i];
}
}
System.out.println("Tong cac phan tu chia het cho 3 la: "+sum);
}
}