Java Basic- Tổng hợp các bài tập ôn luyện java căn bản phần 2
Bài 1.
Viết chương trình java tạo và thực thi theo menu sau:
1. Nhập vào tên của bạn
2. Hiển thị tên vừa nhập.
3. Thoát.
Bài 2. Viết chương trình java thực thi theo menu sau:
1. Nhập vào 2 số nguyên
2. Hiển thị 2 số vừa nhập
3. Tổng 2 số
4. Tích 2 số
5. Hiệu 2 số
6. Thương 2 số.
7. Thoát.
Bài 3. Viết chương trình java thực thi theo menu sau:
1. Nhập vào họ tên của bạn
2. Nhập vào điểm toán lý hóa.
3. Tính tổng 3 môn
4. Tính trung bình 3 môn.
5. Thoát.
Bài 4.
Viết chương trình java cho phép tạo và thực thi theo menu sau:
1. Nhập vào một số nguyên dương n.
2. Tính tổng các số từ 1 đến n
3. Kiểm tra n có là số nguyên tố
4. Kiểm tra n có là số hoàn hảo.
5. In ra các số nguyên tố từ 1 đến n
6. In ra các số hoàn hảo từ 1 đến n.
7. Hiển thị số n thành tích các thừa số nguyên tố.
8. Thoát
(Hiển thị 1 số nguyên dương thành tích các thừa số nguyên tố: n = 24 thì in ra: n = 2^3*3)
Bài 5.
Viết chương trình java tạo và thực thi theo menu sau:
1. Nhập vào số nguyên dương n
2. Tính tổng: 1 + 1/2 + 1/3 + ... + 1/n
3. Tính tổng: 1 + 1/2! + 1/3! + ... + 1/n!
4. Thoát.
Bài 6.
Viết chương trình java tạo và thực thi theo menu sau:
1. Nhập vào số nguyên dương n, số nguyên x bất kỳ
2. Tính tổng: 1 + x + x^2/2! + x^3/3! + ... + x^n / n!
3. Tính tổng: 1 +x – x^2/2! + x^3/3! + ... + (-1)^n-1 * x^n / n!
4. Thoát.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Hoàng Anh [community,C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2021-06-30 03:24:44
//bai 3
System.out.println("Nhập họ tên của bạn: ");
Scanner input = new Scanner(System.in);
String name = input.nextLine();
Scanner point = new Scanner(System.in);
System.out.println("Nhập điểm môn toán:");
double i = point.nextDouble();
System.out.println("Nhập điểm môn lý:");
double j = point.nextDouble();
System.out.println("Nhập điểm môn hóa:");
double k = point.nextDouble();
System.out.format("Tên của bạn là: %s \nĐiểm toán: %f \nĐiểm lý: %f \nĐiểm hóa: %f", name,i,j,k);
System.out.println("Tổng điểm 3 môn = " + (i+j+k));
System.out.println("Trung bình 3 môn = " + (i+j+k)/3);
}
}
![Hoàng Anh [community,C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2021-06-30 03:10:18
Bài 2
/*
* 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 javaapplication4;
import java.util.Scanner;
/**
*
* @author Bom1
*/
public class test {
public static void main(String[] args) {
// bai 2
Scanner num = new Scanner(System.in);
System.out.println("Nhập vào số nguyên thứ nhất: ");
int i = num.nextInt();
System.out.println("Nhập vào số nguyên thứ hai: ");
int j = num.nextInt();
System.out.format("\n2 số bạn vừa nhập là: %d , %d", i,j);
System.out.print("\ntổng 2 số= " + (i+j));
System.out.print("\nhiệu 2 số= " + (i-j));
System.out.print("\ntích 2 số= " + (i*j));
System.out.print("\nthương 2 số= " + (i/j));
}
}
![Hoàng Anh [community,C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2021-06-30 02:59: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 javaapplication4;
import java.util.Scanner;
/**
*
* @author Bom1
*/
public class test {
public static void main(String[] args) {
// bai 1
System.out.println("Nhập vào tên bạn: ");
Scanner n = new Scanner(System.in);
String name = n.nextLine();
System.out.format("Tên của bạn là: %s", name);
}
}
![Đặ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 04:10:29
package javalesson2;
import static java.lang.System.exit;
import java.util.Scanner;
public class ex11 {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
String name = null;
int choose;
System.out.println("Menu:\n"
+ "1. Input Name\n"
+ "2. Show Name\n"
+ "3. Exit.\n"
+ "-------------\n"
+ "Input Here: ");
while(true) {
choose = r.nextInt();
switch(choose) {
case 1:
name = r.nextLine();
break;
case 2:
System.out.println(name);
break;
case 3:
exit(0);
default:
System.out.println("Wrong Input!!!");
break;
}
}
}
}
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
2021-01-22 09:42:45
//Bai 1
/*
* 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 lession22;
import java.util.Scanner;
import static javafx.application.Platform.exit;
/**
*
* @author UserName
*/
public class Ex {
public static void main(String[] args) {
int choose;
String ten = null;
Scanner scan = new Scanner(System.in);
do {
System.out.println("Nhap ten. Choose = 1");
System.out.println("Hien thi ten. Choose = 2");
System.out.println("Chon choose:................. ");
choose = scan.nextInt();
switch (choose) {
case 1:
Scanner input = new Scanner(System.in);
System.out.println("Nhap ten: ");
ten = input.nextLine();
break;
case 2:
System.out.println("Ho ten vua nhap: " + ten);
break;
case 3:
System.out.println("Thoat!!!");
System.exit(0);
break;
default:
break;
}
} while (choose != 3);
}
}
![Tuan ha [APROTRAIN_ADF]](https://www.gravatar.com/avatar/79ddd8a9f939d499e4ed211de85619d5.jpg?s=80&d=mm&r=g)
Tuan ha
2020-06-05 14:53:53
https://drive.google.com/file/d/101F6OFzISoHI5m6CM_JkmMRh62235qH6/view?usp=sharing
![NgNhan [APROTRAIN_ADF]](https://www.gravatar.com/avatar/8c2f7eb13764d610abe695cc366696a6.jpg?s=80&d=mm&r=g)
NgNhan
2020-06-04 03:39:28
Bài 5:
// Bài 5:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter positive integer: ");
int n = Integer.parseInt(scanner.nextLine());
int numertor = 2 * n - 1;
int denomitor = n;
System.out.print("1 + 1/2 + ... + 1/" + n + " = " + numertor + "/" + denomitor);
![NgNhan [APROTRAIN_ADF]](https://www.gravatar.com/avatar/8c2f7eb13764d610abe695cc366696a6.jpg?s=80&d=mm&r=g)
NgNhan
2020-06-04 02:50:16
Bài 4:
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter positive integer: ");
int n = Integer.parseInt(scanner.nextLine());
// Tổng các số 1-> n
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println("Sum 1 -> " + n + ": " + sum);
// Kiểm tra số nguyên tố
int a = 0;
if (n < 2) {
System.out.println(n + "is not a prime number.");
} else {
for (int i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
a++;
}
}
}
if (a == 1) {
System.out.println(n + " is a prime number.");
} else {
System.out.println(n + " is not a prime number.");
}
// Kiểm tra số hoàn hảo
int b = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
b += i;
}
}
if (b == n) {
System.out.println(n + " is a perfect number.");
} else {
System.out.println(n + " is not a perfect number.");
}
// In ra số nguyên tố -> n
System.out.print("Prime from 1 to n: ");
for (int i = 2; i <= n; i++) {
int count = 0;
for (int j = 1; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
count++;
}
}
if (count == 1) {
System.out.print(i + " ");
}
}
// In ra số hoàn hảo -> n
System.out.print("\nPerfect number from 1 to n: ");
for (int i = 6; i <= n; i++) {
int count = 0;
for (int j = 1; j <= i / 2; j++) {
if (i % j == 0) {
count += j;
}
}
if (count == i) {
System.out.print(i + " ");
}
}
// n = tích thừa số nguyên tố
System.out.print("\nn and prime: " + n + " = ");
int n1 = n;
for (int i = 2; i <= n; i++) {
while (n % i == 0) {
if (n1 != n) {
System.out.print("*");
}
n /= i;
System.out.print(i);
}
}
}
}
![NgNhan [APROTRAIN_ADF]](https://www.gravatar.com/avatar/8c2f7eb13764d610abe695cc366696a6.jpg?s=80&d=mm&r=g)
NgNhan
2020-06-03 04:33:50
Bài 3:
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Enter your mark: ");
System.out.print("Math: ");
int math = Integer.parseInt(scanner.nextLine());
System.out.print("\nPhysics: ");
int physics = Integer.parseInt(scanner.nextLine());
System.out.print("\nChemistry: ");
int chemistry = Integer.parseInt(scanner.nextLine());
int sum = math + chemistry + physics;
Double average = sum/3.00;
System.out.println("Name: " + name);
System.out.println("Sum of subjects: " + sum);
System.out.println("Avarage mark: " + average);
// System.out.println("Subtract: " + subtract);
// System.out.println("Multiply: " + multiply);
// System.out.println("Division: " + division);
}
}
![NgNhan [APROTRAIN_ADF]](https://www.gravatar.com/avatar/8c2f7eb13764d610abe695cc366696a6.jpg?s=80&d=mm&r=g)
NgNhan
2020-06-03 03:57:04
Bài 2:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter 1st number: ");
int n1 = Integer.parseInt(scanner.nextLine());
System.out.print("Enter 2nd number: ");
int n2 = Integer.parseInt(scanner.nextLine());
int sum = n1 + n2;
int subtract = n1 - n2;
int multiply = n1 * n2;
int division = n1 / n2;
System.out.println("First number: " + n1);
System.out.println("Second number: " + n2);
System.out.println("Sum: " + sum);
System.out.println("Subtract: " + subtract);
System.out.println("Multiply: " + multiply);
System.out.println("Division: " + division);
}
}