Bài Tâp Java

53
ĐẠI HỌC CÔNG NGHỆ THÔNG TIN Trung Tâm VNIT

description

Bài Tâp Java

Transcript of Bài Tâp Java

H Thng Bi Tp Java Bin son : GV Bi Tin Trngin thoi : 0989995221

I HC CNG NGH THNG TIN

Trung Tm VNIT

Chng 1 Nhp Xut D Liu Trong JavaBi 1 : Nhp v in d liu i vi cc kiu d liu c bn :

import java.io.*;

public class b1_NhapIn_ok

{

public static void main(String[] args) throws IOException

{

int iA;

float fB;

double dX;

String sT;

char ch;

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Nhap chuoi ky tu bat ky sT : ");

sT=in.readLine();

System.out.println("Ban vua nhap chuoi ky tu sT = '" + sT + "'");

System.out.print("Nhap ky tu bat ky ch : ");

ch=(char)in.read();

System.out.println("Ban vua nhap ky tu = '" + ch + "'");

System.out.println("Tien Hanh Nhap DL Kieu So");

while(true)

{

try

{

System.out.print("Nhap so nguyen iA : ");

iA=Integer.parseInt(in.readLine());

System.out.println("Ban vua nhap so nguyen intA = " + iA);

System.out.print("Nhap so thuc fB : ");

fB=Float.parseFloat(in.readLine());

System.out.println("Ban vua nhap so thuc kieu float fB = " + fB);

System.out.print("Nhap so thuc dX : ");

dX=Double.parseDouble(in.readLine());

System.out.println("Ban vua nhap so thuc kieu double dX = " + dX);

break;

}

catch (NumberFormatException en)

{

System.out.println("Error : " + en.toString());

}

}

}

}Chng 2 Cu Trc iu Khin

Bi 1 : Nhp 3 cnh a,b,c. Kim tra xem c lp thnh tam gic khng, nu c th l tam gic g v tnh chu vi - din tch tam gic .

import java.io.*;

public class b2_if_TamGiac

{

public static void main(String[] args) throws Exception

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

int a,b,c;

double cv,dt,p;

System.out.print("Nhap canh a : ");

a=Integer.parseInt(in.readLine());

System.out.print("Nhap canh b : ");

b=Integer.parseInt(in.readLine());

System.out.print("Nhap canh c : ");

c=Integer.parseInt(in.readLine());

if ((a>0) && (b>0) && (c>0) && (a+b>c) && (a+c>b) && (b+c>a))

{

if ((a==b) && (b==c))

{

System.out.println("a,b,c lap thanh tam giac deu");

}

else

if ((a==b) || (b==c) || (a==c))

if ((a*a==b*b+c*c) || (b*b==a*a+c*c) || (c*c==a*a+b*b))

System.out.println("a,b,c lap thanh tam giac vuong can");

else

System.out.println("a,b,c lap thanh tam giac can");

else

if ((a*a==b*b+c*c) || (b*b==a*a+c*c) || (c*c==a*a+b*b))

System.out.println("a,b,c lap thanh tam giac vuong");

else

System.out.println("a,b,c lap thanh tam giac thuong");

cv=a+b+c;

p=cv/2;

dt=Math.sqrt(p*(p-a)*(p-b)*(p-c));

System.out.println("Chu vi tam giac = " + cv);

System.out.println("Dien tich tam giac = " + dt);

}

else

System.out.println("Note : a,b,c khong lap thanh tam giac");

}

}Bi 2 : Bi ton tnh tin in s dngpublic class b2_if_TienDien

{

public static void main(String[] args) throws Exception

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

double csCU, csMOI, sodien, sotien;

System.out.print("Nhap chi so dien cu : ");

csCU=Double.parseDouble(in.readLine());

do

{

System.out.print("Nhap chi so dien moi : ");

csMOI=Double.parseDouble(in.readLine());

}

while (csMOI