关于java的初级问题.回答的朋友我给30分

来源:百度知道 编辑:UC知道 时间:2024/09/22 03:58:30
public class student {

String sex(//这里为什么不能加入对象)throws Exception {
String Stu=null;
if((Stu!="M")||(Stu!="F")){

throw new Exception("性别非法异常");

}
else{
System.out.println("欢迎进入");
}
return Stu;

}
public static void main(String args[]){
student er=new student();
try{
er.sex();
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
System.out.println("请重新填写");
}
}

}
上面的那个地方为什么不能加入对象
我只是想知道,那里为什么不能加入对象,加入了就要报错

代码里有几处错误,看看是不是满足你的要求阿

public class student
{

String sex(String Stu) throws Exception
{
if (!Stu.equals("M") && !Stu .equals("F"))
{
throw new Exception("性别非法异常");
}
else
{
System.out.println("欢迎进入");
}
return Stu;

}

public static void main(String args[])
{
student er = new student();
try
{
er.sex("F");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
finally
{
System.out.println("请重新填写");
}
}

}

把你的源代码贴出来看看~

===你是不是想这样做?
public class student {

String sex(String Stu) throws Exception {
if ((Stu != "M") && (Stu != "F")) {

throw new Exception("性别非法异常");

} else {
System.out.pr