By GokiSoft.com|
10:21 14/07/2021|
Java Basic
[Share Code] Java Basic- OOP - Tạo máy tính cơ bản calculator trong java - C2010G
Java Basic- OOP - Tạo máy tính cơ bản calculator trong java
#Main.java
/*
* 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 lesson05.bt986;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
Calculator cal = new Calculator();
Scanner scan = new Scanner(System.in);
int choose;
// System.out.println("Tong: " + cal.cong(x, y));
// System.out.println("Hieu: " + cal.tru(x, y));
// System.out.println("Tich: " + cal.nhan(x, y));
// System.out.println("Thuong: " + cal.chia(x, y));
do {
showMenu();
choose = scan.nextInt();
if(choose == 5) break;
System.out.println("Nhap x: ");
int x = scan.nextInt();
System.out.println("Nhap y: ");
int y = scan.nextInt();
switch (choose) {
case 1:
System.out.println("Tong: " + cal.cong(x, y));
break;
case 2:
System.out.println("Hieu: " + cal.tru(x, y));
break;
case 3:
System.out.println("Tich: " + cal.nhan(x, y));
break;
case 4:
System.out.println("Thuong: " + cal.chia(x, y));
break;
// case 5:
// System.out.println("Thoat!!!");
// break;
default:
System.out.println("Nhap lai!!!");
break;
}
} while (choose != 5);
}
static void showMenu() {
System.out.println("1. Cong");
System.out.println("2. Tru");
System.out.println("3. Nhan");
System.out.println("4. Chia");
System.out.println("5. Thoat");
System.out.println("Chon: ");
}
}
#Calculator.java
/*
* 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 lesson05.bt986;
/**
*
* @author Diep.Tran
*/
public class Calculator {
public float cong(float x, float y) {
return x + y;
}
public float tru(float x, float y) {
return x - y;
}
public float nhan(float x, float y) {
return x * y;
}
public float chia(float x, float y) {
if(y == 0) {
System.out.println("Yeu cau y khac 0!!!");
return 0;
}
return x / y;
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)