简易问题,求教! 其实是散分。

cceyjames 2005-06-01 11:33:29
做了一个方法:

public static boolean isStandard(String src) throws Exception
{
boolean isError = false;
String p[]=src.spilt("-");
String p1 =p[0];
String p2 =p[1];
if (!p1.equals(p2))
{
isError = true;
break;
}
return isError;
}

判断p1是否和p2相等。返回一个boolean。

我想在在这个方法的基础之上增加一个功能:
在p1不等于p2的情况下,返回p1和p2的值。

如何在这个方法中实现?可能要更改方法的返回方式。

我是菜鸟!



昨天加班到2点,今天特来散分!
...全文
114 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
cceyjames 2005-06-01
  • 打赏
  • 举报
回复
嘿嘿,可是我返回的只能是boolean的呀,无法保存string
kiover 2005-06-01
  • 打赏
  • 举报
回复
haha,我接,返回的对象能存放你所需要的东东就行
dophin1003 2005-06-01
  • 打赏
  • 举报
回复
可以返回个List
dophin1003 2005-06-01
  • 打赏
  • 举报
回复
接分
cceyjames 2005-06-01
  • 打赏
  • 举报
回复
我考,都要分哈?嗬嗬
好,现在散分。
cceyjames 2005-06-01
  • 打赏
  • 举报
回复
zhaohb1980() 的想法和我不谋而合。嘿嘿。我已经做好了,谢谢大家!
life8love 2005-06-01
  • 打赏
  • 举报
回复
和楼上动机一样
说好不能打脸 2005-06-01
  • 打赏
  • 举报
回复
问题应该解决了吧,我来接分的
曼绕木杉 2005-06-01
  • 打赏
  • 举报
回复
public static String isStandard(String src) throws Exception
{
boolean isError = false;
String p[]=src.spilt("-");
String p1 =p[0];
String p2 =p[1];
String str_return = p1 + "-" + p2;

if (!p1.equals(p2))
{
isError = true;
break;
}

if (isError)
{
str_return = "true";
}

return str_return;
}

再调用此函数的地方对你的返回值进行分析,看其是否equals("true")
下边的我就不用说了吧^_^
cceyjames 2005-06-01
  • 打赏
  • 举报
回复
土豆搬家的写法是将这个实现功能写成了一个类了。
我只是想做一个方法即可,放入到项目中的某个tools类来调用。
cceyjames 2005-06-01
  • 打赏
  • 举报
回复
按照AreamArgentateOfWing(梦幻银翼) 的方法,编译的时候报错:
non-static variable this cannot be referenced from a static context this.p1 = p1;

non-static variable this cannot be referenced from a static context this.p2 = p2;
liuzhijie0451 2005-06-01
  • 打赏
  • 举报
回复
我觉的“ OnlyFor_love(【土豆搬家】) ”写的没有问题
jxdn_yang 2005-06-01
  • 打赏
  • 举报
回复
get
OnlyFor_love 2005-06-01
  • 打赏
  • 举报
回复
很简单啊,你可以单独写一个类用来返回这两个string的值,
然后在你的isStandard(String src)方法里做一下判断,如果相等就返回true,如果不相等就返回false,并且调用你写的那个类就可以了!
以下是类似的程序,不过功能和你的一样,你稍微改改就可以了!
public class a
{
public static void main(String [] args){
a test = new a();
test.isStandard("abcd","acbd");

}

public static boolean isStandard(String str1,String str2)
{
boolean isError = false;
String temp1 =str1;
String temp2 =str2;
if (temp1.equals(temp2))
{
isError = true;
return isError;
}
else{
a.iffalse(str1);
return isError;


}
}
public static String iffalse(String a){
System.out.println(a);
return a;

}
}
lindd 2005-06-01
  • 打赏
  • 举报
回复
private String p1;
private String p2;
public static boolean isStandard(String src) throws Exception
{
this.p1 =src.spilt("-")[0];
this.p2 =src.spilt("-")[1];
return (!p1.equals(p2));
}//
kaiser_800 2005-06-01
  • 打赏
  • 举报
回复
用get方法!接分。
galewithwing 2005-06-01
  • 打赏
  • 举报
回复
这个相等有点说法,第一,数组长度相等,第二,其中内容相等;
如果是一般对象,还有其他说法
lg820505 2005-06-01
  • 打赏
  • 举报
回复
不错
接分
  • 打赏
  • 举报
回复
private String p1;
private String p2;

public static boolean isStandard(String src) throws Exception {
boolean isError = false;
String p[] = src.spilt("-");
String p1 = p[0];
String p2 = p[1];
this.p1 = p1;
this.p2 = p2;
if (!p1.equals(p2)) {
isError = true;
break;
}
return isError;
}

public String getP1() {
return this.p1;
}

public String getP2() {
return this.p2;
}
这样可以了吧?

81,092

社区成员

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

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