62,621
社区成员
发帖
与我相关
我的任务
分享
public class Student {
private String name;
private int age;
private char gender;
private String phone;
private String address;
private String email;
void SetName(String name){
this.name = name;
}
String GetName(){
//System.out.println("The Student Name is:" + this.name);
return this.name;
}
void SetAge(int age){
this.age = age;
}
int GetAge(){
//System.out.println("The Student Age is:" + this.age);
return this.age;
}
void SetGender(char gender){
this.gender = gender;
}
char GetGender(){
//System.out.println("The Student Gender is:" + this.gender);
return this.gender;
}
void SetPhone(String phone){
this.phone = phone;
}
String GetPhone(){
//System.out.println("The Student Phone is:" + this.phone);
return this.phone;
}
void SetAddress(String address){
this.address = address;
}
String GetAddress(){
//System.out.println("The Student Address is:" + this.address);
return this.address;
}
void SetEmail(String email){
this.email = email;
}
String GetEmail(){
//System.out.println("The Student Email is:" + this.email);
return this.email;
}
Student(String name, int age, char gender, String phone, String address, String email){
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
this.address = address;
this.email = email;
}
void Eat(){
System.out.println(this.GetName() + " is eating!");
}
void Drink(){
System.out.println(this.GetName() + " is drinking!");
}
void Play(){
System.out.println(this.GetName() + " is playing!");
}
void Sleep(){
System.out.println(this.GetName() + " is sleeping!");
}
}
public class Contact {
private Student[] contact;
Contact(Student[] contact){
this.contact = contact;
}
String QueryByName(String name){
for(int i = 0; i < this.contact.length; i++){
if(this.contact[i].GetName() == name)
return this.contact[i].GetName();
else
return "No this Student";
}
}
String QueryByEmail(String email){
for(int i = 0; i < this.contact.length; i++){
if(this.contact[i].GetEmail() == email)
return this.contact[i].GetEmail();
else
return "No this Student";
}
}
String QueryByAddress(String address){
for(int i = 0; i < this.contact.length; i++){
if(this.contact[i].GetAddress() == address)
return this.contact[i].GetAddress();
else
return "No this Student";
}
}
String QueryByPhone(String phone){
for(int i = 0; i < this.contact.length; i++){
if(this.contact[i].GetPhone() == phone)
return this.contact[i].GetPhone();
else
return "No this Student";
}
}
}
public class Practice {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu1 = new Student("Tom", 31, 'M', "13100000000", "Road 1", "tom@sina.com");
Student stu2 = new Student("Jerry", 32, 'F', "13200000000", "Road 2", "jerry@sina.com");
Student stu3 = new Student("Steve", 33, 'M', "13300000000", "Road 3", "steve@sina.com");
Student stu4 = new Student("Tiger", 34, 'F', "13400000000", "Road 4", "tiger@sina.com");
Student[] c = new Student[]{stu1,stu2,stu3,stu4};
Contact contact = new Contact(c);
/*
for(int i = 0; i < contact.length; i++){
System.out.println("Student Name is:" + contact[i].GetName() + "; Address is:" + contact[i].GetAddress() + "; Phone is:" + contact[i].GetPhone());
}
*/
contact.QueryByName("Tom");
contact.QueryByEmail("tom@sina.com");
contact.QueryByAddress("Road 3");
contact.QueryByPhone("13300000000");
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type String
at Contact.QueryByName(Contact.java:9)
at Practice.main(Practice.java:25)String QueryByName(String name){
for(int i = 0; i < this.contact.length; i++){
if(this.contact[i].GetName().equals(name)){
return this.contact[i].GetName();
}
}
return "No this Student";
}ceshi