新手问题--!!求大神指点!!java代码,错误在哪里??实在找不出来

littleshoes 2017-01-04 05:36:45

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)


我用的是Myeclips10,jre 1.8.0_60
...全文
581 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
逸花城 2017-01-10
  • 打赏
  • 举报
回复
你的逻辑错误,应该这样写!
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";
}
pake_2016 2017-01-05
  • 打赏
  • 举报
回复
建议楼主写代码的时候把修饰符都写上,要不然看的很别扭
___d 2017-01-05
  • 打赏
  • 举报
回复
字符串比较用equals, 因为你用的==导致没走return,方法没return 就报这个错了
littleshoes 2017-01-05
  • 打赏
  • 举报
回复
引用 2 楼 m2200 的回复:
Contact里面的四个方法写的都有问题,你应该在for循环体后面再加一个return ...;才对,因为编译器会判断出你很可能进入不了for循环体中,那么你后面没有return语句的话,就不符合这个方法必须返回个字符串类型对象的要求了,所以编译时就会出错。
very感谢
littleshoes 2017-01-05
  • 打赏
  • 举报
回复
引用 3 楼 ryuugu_rena 的回复:
at Contact.QueryByName(Contact.java:9) 第9行出问题了,字符串的比较用equals,用什么==
very 感谢
chongchong517 2017-01-04
  • 打赏
  • 举报
回复
ceshi
鬼善 2017-01-04
  • 打赏
  • 举报
回复
数组可以Student[]定义吗!换成arraylist[student] 应该就行了!
ryuugu_rena 2017-01-04
  • 打赏
  • 举报
回复
at Contact.QueryByName(Contact.java:9) 第9行出问题了,字符串的比较用equals,用什么==
爱睡觉的阿狸 2017-01-04
  • 打赏
  • 举报
回复
Contact里面的四个方法写的都有问题,你应该在for循环体后面再加一个return ...;才对,因为编译器会判断出你很可能进入不了for循环体中,那么你后面没有return语句的话,就不符合这个方法必须返回个字符串类型对象的要求了,所以编译时就会出错。
gzcitizeny 2017-01-04
  • 打赏
  • 举报
回复
根据错误提示,以下行出错。 for(int i = 0; i < this.contact.length; i++){ 和 contact.QueryByAddress("Road 3");

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧