By GokiSoft.com| 15:29 14/06/2023|
Java Basic

Java Basic- Ôn luyện về vòng lặp for, while, do-while trong java BT978

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.

Liên kết rút gọn:

https://gokisoft.com/978

Bình luận

avatar
Trần Thị Khánh Huyền [T2008A]
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]+", ");
    }
    
}
}   


avatar
Đặng Trần Nhật Minh [T2008A]
2021-01-22 10:00:45


Bai 3
/*
 * 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);
        }
    }
    
}


avatar
Đặng Trần Nhật Minh [T2008A]
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);
    }
    
}


avatar
Đặng Trần Nhật Minh [T2008A]
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);
    }
    
}


avatar
Nguyễn Tiến Đạt [T2008A]
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] + " ");
        }
    }
}


avatar
vuong huu phu [T2008A]
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);
    
    
}}


avatar
NgNhan [APROTRAIN_ADF]
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] + "  ");
        }


avatar
Lê Xuân Dũng [JavaFree]
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");
        }
    }
    
}


avatar
Lê Xuân Dũng [JavaFree]
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);
	}

}


avatar
Phạm Kim Anh [JavaFree]
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);
	}
}