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)
![Nguyen Tri Duc [java1_online]](https://www.gravatar.com/avatar/9ad80c7352fd64a202fbfb45d9ca9fb1.jpg?s=80&d=mm&r=g)
Nguyen Tri Duc
2023-04-15 17:23:49
Chưa học mảng mà đã có bài tập khai báo mảng rồi. Không hiểu thứ tự học của chương trình.
![Trần Nhựt Linh [java1_online]](https://www.gravatar.com/avatar/6e945f8e29edcd38ec9b7492c0265f02.jpg?s=80&d=mm&r=g)
Trần Nhựt Linh
2023-03-29 15:43:09
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.onluyenvonglap;
import java.util.Scanner;
/**
*
* @author mymem
*/
public class Onluyenvonglap {
public static void main(String[] args) {
//bai 1
// int[] t = new int[10];
// Scanner sc = new Scanner(System.in);
// for(int i=0;i<10;i++){
// System.out.println("Nhap gia tri 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 cua 10 phan tu tren la:" + sum);
//bai 2
// int n;
// Scanner sc = new Scanner(System.in);
// System.out.println("Nhap so luong phan tu:");
// n= sc.nextInt();
// int[] t = new int[n];
// for(int i=0;i<n;i++){
// System.out.println("Nhap gia tri phan tu thu " + (i+1));
// t[i]=sc.nextInt();
// }
// int sum=0;
// for(int i=0;i<n;i++){
// if((t[i]%3)==0){
// sum=sum+t[i];
// }
// }
// System.out.println("Tong cua cac phan tu chia het cho 3 la:" + sum);
//bai3
// int n;
// Scanner sc = new Scanner(System.in);
// System.out.println("Nhap N:");
// n= sc.nextInt();
// for(int i=0;i<n;i++){
// for(int j=0;j<=i;j++){
// System.out.print("*");
// }
// System.out.println("");
// }
//bai4
// int n;
// Scanner sc = new Scanner(System.in);
// System.out.println("Nhap N:");
// n= sc.nextInt();
// int f1=0;
// int f2=1;
// int fn=1;
//
// System.out.println("Day so fobinaci < N: ");
// System.out.println("1");
// while(fn<n){
// System.out.println("" + fn);
// f1=f2;
// f2=fn;
// fn=f1+f2;
// }
//bai5
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Nhap N:");
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 temp;
for(int i =0;i<n;i++){
for(int j=(i+1);j<n;j++){
if(t[i]>t[j]){
temp=t[i];
t[i]=t[j];
t[j]=temp;
}
}
}
System.out.println("Day so tang dan:");
for(int i=0;i<n;i++){
System.out.println("" + t[i]);
}
}
}
![Hoàng Anh [C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2022-06-27 18:31:32
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package BaiTap.bai978;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class bai3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Nhập N: ");
int N = Integer.parseInt(input.nextLine());
for(int i =0; i <= N; i++){
for(int j =0 ; j <=i; j++){
System.out.print("*");
}
System.out.println("");
}
}
}
![Hoàng Anh [C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2022-06-27 14:08:34
/*
* 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 baitap978;
import java.util.Scanner;
/**
*
* @author by bom1
*/
public class bai2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Mảng gồm số phần tử là: ");
int N = Integer.parseInt(input.nextLine());
int[] arr = new int[N];
for (int i =0; i< arr.length; i++){
System.out.println("Nhập giá trị thứ " +(i+1)+ " cho mảng:");
arr[i] = Integer.parseInt(input.nextLine());
}
int sum =0;
for (int i =0; i< arr.length; i++){
if (arr[i] %3 == 0){
sum = sum + arr[i];
}
}
System.out.println("Tổng các phần tử chia hết cho 3 trong mảng: " + sum);
}
}
![Hoàng Anh [C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2022-06-27 13:55:01
/*
* 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 javaapplication1;
import java.util.Scanner;
/**
*
* @author by bom1
*/
//BAI 1
public class baitap978 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Nhập vào mảng 10 phần tử: ");
int[] arr = new int[10];
for (int i =0; i < arr.length; i++){
System.out.println("Nhập vào mảng cho giá trị thứ " +(i+1)+ ":");
arr[i] = Integer.parseInt(input.nextLine());
}
int sum =0;
for (int i =0; i < arr.length; i++){
sum = sum + arr[i];
}
System.out.println("Tổng giá trị các phần tử trong mảng:" + sum);
}
}
![Phạm Đăng Khoa [community,C2010L]](https://www.gravatar.com/avatar/c38babdc190b58e31608b8ddefc3ba1a.jpg?s=80&d=mm&r=g)
Phạm Đăng Khoa
2021-07-01 17:13:23
/*
* 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 main;
import java.util.Scanner;
/**
*
* @author Khoa Pham
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 0;
System.out.println("Nhap n : ");
n = sc.nextInt();
int[] arr = new int[n];
for(int i= 0 ; i <n ; i++){
arr[i] = sc.nextInt();
}
int temporary;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < i ; j++){
if(arr[i] < arr[j]){
temporary = arr[i];
arr[i] = arr[j];
arr[j] = temporary;
}
}
}
for(int i=0 ; i<n ; i++){
System.out.print(arr[i]+", ");
}
}
}
![Phạm Đăng Khoa [community,C2010L]](https://www.gravatar.com/avatar/c38babdc190b58e31608b8ddefc3ba1a.jpg?s=80&d=mm&r=g)
Phạm Đăng Khoa
2021-07-01 17:04:20
/*
* 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 main;
import java.util.Scanner;
/**
*
* @author Khoa Pham
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 0;
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("*");
}
}
}
![Phạm Đăng Khoa [community,C2010L]](https://www.gravatar.com/avatar/c38babdc190b58e31608b8ddefc3ba1a.jpg?s=80&d=mm&r=g)
Phạm Đăng Khoa
2021-07-01 16:55:56
/*
* 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 main;
import java.util.Scanner;
/**
*
* @author Khoa Pham
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 0;
System.out.println("Nhap so phan tu : ");
n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0 ; i < n ; i++) {
System.out.println("Nhap cac phan tu thu: " +(i+1));
arr[i]= sc.nextInt();
}
int sum = 0;
for( int i=0 ; i< n ; i++){
if(arr[i]%3==0){
sum = sum + arr[i];
}
}
System.out.println("Tong cac phan tu chia het cho 3 trong mang la: " +sum);
}
}
![Phạm Đăng Khoa [community,C2010L]](https://www.gravatar.com/avatar/c38babdc190b58e31608b8ddefc3ba1a.jpg?s=80&d=mm&r=g)
Phạm Đăng Khoa
2021-07-01 15:42:38
/*
* 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 main;
import java.util.Scanner;
/**
*
* @author Khoa Pham
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
for( int i = 0 ; i < 10 ; i++) {
System.out.println("Nhap cac phan tu thu: " +(i+1));
arr[i]= sc.nextInt();
}
int sum = 0;
for( int i=0 ; i< 10 ; i++){
sum = sum + arr[i];
}
System.out.println("Tong cac phan tu trong mang la: " +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-25 03:17:49
Bai 4
package javalesson2;
import java.util.Scanner;
public class ex09 {
public static int Fibonacci (int n) {
if (n < 3) return 1;
else return Fibonacci(n - 1) + Fibonacci(n - 2);
}
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
int maxx = r.nextInt(), n = 0;
while (Fibonacci(n) < maxx) System.out.println("F(" + n + ") = " + Fibonacci(n++));
}
}