如果不等于以上所有情况会出现什么结果呢?是不是没有返回啊?所以,应该是这样的:
class A
{
public static int oprToInteger(String a)//操作符转换成整数
{
if(a=="+") return 0;
else if (a=="*") return 1;
else if (a=="(") return 2;
else if (a==")") return 3;
else if (a=="|") return 4;
else if (a=="#") return 5;
return 6; // <-就是这里了
}
public static void main(String[] args)
{
System.out.println(oprToInteger("+"));
}
}
class A
{
public static int oprToInteger(String a)//操作符转换成整数
{
if(a=="+") return 0;
else if (a=="*") return 1;
else if (a=="(") return 2;
else if (a==")") return 3;
else if (a=="|") return 4;
else if (a=="#") return 5;
return 6; // <-就是这里了
}
public static void main(String[] args)
{
System.out.println(oprToInteger("+"));
}
}
解决方案 »
- 接口的好处在哪儿
- java连接远程access问题 。。。。。
- java的输出的现象
- 能回答问题者得100,先到先得!
- 请求帮助!!
- 这样的系统如何实现?
- 刚下载了javaDocumunation不知道怎么用
- 谁能把这个Applet下载下来吗?我用NetAnts不行。请指教!http://www4.cmbchina.com/javahq/fenshi.htm
- 各位高手:我想学习JAVA,听说jbuilder不错,请介绍几本入门书,多给一点见意。谢谢。
- 初学JAVA 请问文件里的内容为什么要用while循环读取
- 小虾问大虾,你都知道啥!!!(有关序列化)
- java中建立oracle连接的问题(oci和thin方式的区别、优劣?)
所以,你可以:
1、在最后加一个 return nRet; (象说的一样)。
2、也可以加一个else return nRet; 。
供参考
if(a.compareTo("+"))==0) return 0;
else .....
试试这个!
(a=="+")
这样是的不到正确结果的!!!,应该这样:
a.equals("+")
才行的!!!
///////////////
另外,看你的参数,都是判断单字符的,何不象 foulong(龙行天下) 说的
用 swith with char?
import java.lang.*;
class A
{
public static int oprToInteger(String a)//操作符转换成整数
{
if(a=="+") return 0;
else if (a=="*") return 1;
else if (a=="(") return 2;
else if (a==")") return 3;
else if (a=="|") return 4;
else if (a=="#") return 5;
return 6;
}
public static void main(String[] args)
{
char[] a=new char[10];
a[0]='*';
Stack s=new Stack();
s.push(String.valueOf(a[0]));
System.out.println(oprToInteger((String)s.peek()));
}
}结果为6。我认为应该为1。为什么?
if(a.equals("+")……
输出时好象也有问题,
System.out.println(toString(oprToInteger("+")));
你试一下~~~
if(a.equals("+")……在末尾加上return -1;或者别的!