Lời giải tham khảo cho OOP2 nhập từ bàn phím
Bài trước (http://start.formyjob.net/forum-f15/topic-t198.htm) mình đã giải nhưng không đưa ra giải pháp nhập dữ liệu từ bàn phím
Nay xin được bổ sung như sau :
- Code:
-
package firstproject;
import java.lang.*;
import java.io.*;
public class StringTester
{
public static void main (String args[]) throws IOException
{
BufferedReader input_Obj=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Student one (Fullname-Birthday) : ");
String student1=input_Obj.readLine();
System.out.println("Student two (Fullname-Birthday) : ");
String student2=input_Obj.readLine();
String tmp1=student1.trim();
String[] splitarray1=tmp1.split("-");
String[] name1=splitarray1[0].split(" ");
String tmp12=splitarray1[1].trim();
String[] birthday1=tmp12.split("/");
int dd1=Integer.parseInt(birthday1[0]);
int mm1=Integer.parseInt(birthday1[1]);
int yy1=Integer.parseInt(birthday1[2]);
String tmp2=student2.trim();
String[] splitarray2=tmp2.split("-");
String[] name2=splitarray2[0].split(" ");
String tmp22=splitarray2[1].trim();
String[] birthday2=tmp22.split("/");
int dd2=Integer.parseInt(birthday2[0]);
int mm2=Integer.parseInt(birthday2[1]);
int yy2=Integer.parseInt(birthday2[2]);
System.out.println("Full name of Student 1 : "+splitarray1[0]);
System.out.println("Birthday : "+splitarray1[1]);
System.out.println("First name : "+name1[0]);
int length1=name1.length;
System.out.println("Last name : "+name1[length1-1]);
System.out.println("Full name of Student 2 : "+splitarray2[0]);
System.out.println("Birthday : "+splitarray2[1]);
System.out.println("First name : "+name2[0]);
int length2=name2.length;
System.out.println("Last name : "+name2[length2-1]);
if (name1[0].equals(name2[0])) System.out.println("Two Students's First Name is the same : "+name1[0]);
else System.out.println("The Student's First Names is diferent");
if (yy1>yy2) System.out.println("Youngest Student is : "+splitarray1[0]);
if (yy1<yy2) System.out.println("Youngest Student is : "+splitarray2[0]);
if (yy1==yy2)
{
if (mm1>mm2) System.out.println("Youngest Student is : "+splitarray1[0]);
if (mm1<mm2) System.out.println("Youngest Student is : "+splitarray2[0]);
else
{
if (dd1>=dd2) System.out.println("Youngest Student is : "+splitarray1[0]);
if (dd1<dd2) System.out.println("Youngest Student is : "+splitarray2[0]);
}
}
}
}
Kết xuất của trương trình :
run:
Student one (Fullname-Birthday) :
nguyen thi lien-23/12/1989
Student two (Fullname-Birthday) :
nguyen thi lan huong-22/11/1988
Full name of Student 1 : nguyen thi lien
Birthday : 23/12/1989
First name : nguyen
Last name : lien
Full name of Student 2 : nguyen thi lan huong
Birthday : 22/11/1988
First name : nguyen
Last name : huong
Two Students's First Name is the same : nguyen
Youngest Student is : nguyen thi lien
BUILD SUCCESSFUL (total time: 30 seconds)
Mời các bạn gửi thêm giải pháp mới cho bài toán này để mọi người học tập nhau !