新手问题--!!求大神指点!!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
...全文
582 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");
【期刊论文复现】多元宇宙算法解电力系统多目标优化问题(Matlab实现)内容概要:本文复现了期刊论文中提出的多元宇宙算法(Multi-Verse Optimizer, MVO)用于解电力系统中的多目标优化调度问题,采用Matlab进行代码实现。该算法受宇宙中黑洞、白洞和虫洞等天文现象启发,通过数学建模模拟多目标搜索过程,具有较强的全局寻优能力和收敛性能。文中将其应用于典型的电力系统经济调度场景,如考虑燃料成本、排放量等多个优化目标的微电网或多能源系统调度问题,通过构建合理的数学模型和约束条件,实现对多个冲突目标的协同优化。同时,文章提供了完整的代码框架与仿真结果,便于读者理解和复现。; 适合人群:具备一定Matlab编程基础和优化算法基础知识的研究生、科研人员及从事电力系统优化调度工作的工程师。; 使用场景及目标:①学习和掌握新型智能优化算法——多元宇宙算法的基本原理与实现方法;②将其应用于电力系统经济调度、微电网优化、多目标决策等领域的模型解;③为科研工作提供可复现的代码参考和技术支持。; 阅读建议:建议读者结合Matlab代码逐行理解算法实现细节,重点关注目标函数建模、约束处理及MVO算法核心迭代逻辑,并尝试在不同测试系统中进行参数调优与性能对比,以深入掌握其应用技巧。
内容概要:本文针对计及光伏波动性的主动配电网有功无功协调优化问题,提出一种基于多目标粒子群优化算法(MOPSO)的协同优化方案,并通过Matlab代码实现仿真验证。研究充分考虑光伏发电的随机性与间歇性特征,构建了一个综合有功功率调节、无功功率补偿、电压质量提升及网络损耗最小化的多目标优化模型,旨在提升高渗透率光伏接入下主动配电网的运行稳定性与经济性。通过优化算法解实现有功与无功资源的协调调度,有效缓解因光伏出力波动引起的电压越限与潮流分布不均等问题,增强了系统的调节能力与鲁棒性。; 适合人群:具备电力系统分析基础和Matlab编程能力的科研人员,以及电气工程、自动化等相关专业的研究生和高年级本科生。; 使用场景及目标:①用于研究高比例可再生能源接入背景下主动配电网的优化运行策略;②为电力系统仿真教学与科研提供可复现的算法模型与代码实例,目标是掌握多目标智能优化算法在电力系统优化调度中的建模方法、实现流程及其工程应用价值。; 阅读建议:建议读者结合Matlab代码逐段理解算法实现细节,重点关注目标函数的设计、约束条件的处理方式以及粒子群算法的改进策略,同时可通过调整参数设置、测试不同场景以深入理解优化机制与系统响应特性。

62,620

社区成员

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

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