[问题] 一个java的小问题,不过我似乎就是搞不懂

陪你去看龙卷风

新手上路
VIP
注册
2002-10-12
消息
11,271
荣誉分数
61
声望点数
0
是我的一个作业,用stack做expression,不过我不明白,代码没啥问题啊,怎么就是编译时候老是告诉我constant expression required
:hippie:

代码:
	public final static int UNARY = "#".hashCode();
	public final static int SIN = "sin".hashCode();
	public final static int COS = "cos".hashCode();
	public final static int TAN = "tan".hashCode();
	public final static int LOG = "log".hashCode();
	public final static int EXP = "exp".hashCode();
	public final static int POW = "^".hashCode();
	public final static int MUL = "*".hashCode();
	public final static int DIV = "/".hashCode();
	public final static int ADD = "+".hashCode();
	public final static int SUB = "-".hashCode();

	public double eval(){
...........................
		
		while (!opStack.isEmpty()){
			stackValA = ((Double)valStack.pop()).doubleValue();
			int opTrigger = ((opStack.pop()).toString()).hashCode();
			opTriggerHandle(opTrigger);
		}
		return 0;
	}
	public void opTriggerHandle(int opTrigger)
	{
	   	switch (opTrigger) 
	   	{
	   		case UNARY	: Unary();		break; //unary operator
			case SIN	: Sine();		break; //sin
			case COS	: Cosine();		break; //cos
			case TAN	: Tangent();	break; //tan
			case LOG	: Logarithm();	break; //log
			case EXP	: PowerOfe();	break; //exp
			default:
				stackValB = ((Double)valStack.pop()).doubleValue();
				switch (opTrigger) 
	   			{
	   				case POW: Power(); 				break; //pow
    				case MUL: Multiplication();		break; //product
    				case DIV: Division();			break; //divide
    				case ADD: Addition();			break; //plus
    				case SUB: Subtraction();		break; //minus
    			} 
    			break;
    	}
	}
--------------------Configuration: <Default>--------------------
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:92: constant expression required
case UNARY : Unary(); break; //unary operator
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:93: constant expression required
case SIN : Sine(); break; //sin
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:94: constant expression required
case COS : Cosine(); break; //cos
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:95: constant expression required
case TAN : Tangent(); break; //tan
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:96: constant expression required
case LOG : Logarithm(); break; //log
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:97: constant expression required
case EXP : PowerOfe(); break; //exp
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:102: constant expression required
case POW: Power(); break; //pow
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:103: constant expression required
case MUL: Multiplication(); break; //product
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:104: constant expression required
case DIV: Division(); break; //divide
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:105: constant expression required
case ADD: Addition(); break; //plus
^
C:\Documents and Settings\Torune\Desktop\2402\Expression.java:106: constant expression required
case SUB: Subtraction(); break; //minus
^
11 errors

Process completed.
 
这样的话,就没问题,哎。。。。
代码:
public class TestSwitch { 

     public final static int A = 0; 
     public final static int B = 1; 
     public final static int C = 2; 

     public static void show(int i) { 
         switch(i) { 
             case A:
                 System.out.println(A); 
             case B: 
                 System.out.println(B+B); 
             default: 
                 System.out.println(C+C+C); 
         } 
     } 

     public static void main(String[] args) { 
         show(1); 
     } 

}
 
the hashcode is generated by VM based on object runtime address. the compiler may not consider it as a valid constant.
i guess, :)
plus, there is no guaranty that a unique number will be returned by the method. so, String plus if-elseif...else may be better. or use HashMap
 
well, i hard coded the hashCode parts, it works right now.
as long as my TA is okey with these things, i wont change it :)
hope they can understand how to finish this assignment, coz they just whole bunch of sh!t.
 
最初由 bbj 发布
the hashcode is generated by VM based on object runtime address. the compiler may not consider it as a valid constant.
i guess, :)
plus, there is no guaranty that a unique number will be returned by the method. so, String plus if-elseif...else may be better. or use HashMap
我们不让用hashmap, hardcode能用的话我就不想改了:p :D
 
Hi,

In java switch-case, please note that the "case expression" must be a constant value or constant expression.The compiler has to be able to determine this "case expression" value at compile rather than run‑time.
In your case,
public final static int UNARY = "#".hashCode();
public final static int SIN = "sin".hashCode();
public final static int COS = "cos".hashCode();
public final static int TAN = "tan".hashCode();
public final static int LOG = "log".hashCode();
public final static int EXP = "exp".hashCode();
public final static int POW = "^".hashCode();
public final static int MUL = "*".hashCode();
public final static int DIV = "/".hashCode();
public final static int ADD = "+".hashCode();
public final static int SUB = "-".hashCode();
The value of those static constants are not computed until your class are first loaded. In other word, at compile time, those values are not "constant".

Hope these helps.

Cheers.
 
最初由 javatutor 发布
Hi,

In java switch-case, please note that the "case expression" must be a constant value or constant expression.The compiler has to be able to determine this "case expression" value at compile rather than run‑time.
In your case,

The value of those static constants are not computed until your class are first loaded. In other word, at compile time, those values are not "constant".

Hope these helps.

Cheers.
ya thats why i hardcoded those hashcode values.

Regards.


//35 -> "#".hashCode();
//113880 -> "sin".hashCode();
//98695 -> "cos".hashCode();
//114593 -> "tan".hashCode();
//107332 -> "log".hashCode();
//100893 -> "exp".hashCode();
//94 -> "^".hashCode();
//42 -> "*".hashCode();
//47 -> "/".hashCode();
//43 -> "+".hashCode();
//45 -> "-".hashCode();

//40 -> "(".hashCode();
//41 -> ")".hashCode();
//91 -> "[".hashCode();
//93 -> "]".hashCode();
//123 -> "{".hashCode();
//125 -> "}".hashCode();


public final static int UNARY = 35;
public final static int SIN = 113880;
public final static int COS = 98695;
public final static int TAN = 114593;
public final static int LOG = 107332;
public final static int EXP = 100893;
public final static int POW = 94;
public final static int MUL = 42;
public final static int DIV = 47;
public final static int ADD = 43;
public final static int SUB = 45;

public final static int SBa = 40;
public final static int EBa = 41;
public final static int SBb = 91;
public final static int EBb = 93;
public final static int SBc = 123;
public final static int EBc = 125;
 
最初由 javatutor 发布
Hi,

In java switch-case, please note that the "case expression" must be a constant value or constant expression.The compiler has to be able to determine this "case expression" value at compile rather than run‑time.
In your case,

The value of those static constants are not computed until your class are first loaded. In other word, at compile time, those values are not "constant".

Hope these helps.

Cheers.

very clear, :cool:
 
后退
顶部