如果不等于以上所有情况会出现什么结果呢?是不是没有返回啊?所以,应该是这样的:
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("+"));
}
}

解决方案 »

  1.   

    why not swith with char?
      

  2.   

    对呀,如果都不是你所列举的情况,不就没有正确的返回了吗!
    所以,你可以:
    1、在最后加一个 return nRet; (象说的一样)。
    2、也可以加一个else return nRet; 。
    供参考
      

  3.   

    你的判断条件有些问题,String型的变量不能直接比较,应该改成:
    if(a.compareTo("+"))==0) return 0;
       else .....
    试试这个!
      

  4.   

    哎呀,还有呀,你的条件判断
    (a=="+") 
    这样是的不到正确结果的!!!,应该这样:
    a.equals("+")
    才行的!!!
    ///////////////
    另外,看你的参数,都是判断单字符的,何不象 foulong(龙行天下) 说的
    用 swith with char?
      

  5.   

    import java.util.*;
    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。为什么?
      

  6.   

    判断条件
    if(a.equals("+")……
    输出时好象也有问题,
    System.out.println(toString(oprToInteger("+")));
    你试一下~~~
      

  7.   

    楼上都说了!
    if(a.equals("+")……在末尾加上return -1;或者别的!