回 帖 发 新 帖 刷新版面

主题:请大家帮我看看这个问题出在哪里

import java.util.*;
public class JISUAN
{
    public static void main(String args[])    
    {
        int i=0;
        Stack<Float> q1 = new Stack<Float>();
        Stack<Float> q2 = new Stack<Float>();
        String str = "(523.75*(-5+1)+2*7)"; 
        char s[]=str.toCharArray();
        //System.out.println(s.length);
        for(i=0;i<s.length;i++)
        {
                char data;
                data = s[i]    ;
    //解决负数问题,在负数前面加0
                if(data=='(')
                {
                    if(s[i+1]=='-')
                    {
                        q1.push(-5.00f);
                        //System.out.println("-");
                        q1.push(0.00f);    
                        //System.out.println("0.00f");
                        //System.out.println("(后有负号");
                    }
                    else
                    {
                        q1.push(-5.00f);
                        //System.out.println("(");
                        //System.out.println("(后无负号");    
                    }                                    
                }
//处理数字
                else if(data=='0'||data=='1'||data=='2'||data=='3'||data=='4'||data=='5'||data=='6'||data=='7'||data=='8'||data=='9')
                {
                
                    if(s[i+1]=='+'||s[i+1]=='-'||s[i+1]=='*'||s[i+1]=='/'||s[i+1]==')')
                    {    
                        //System.out.println(s[i]);
                        float x = s[i];
                        //System.out.println(x);    
                        q1.push(x-48.0f);//单个数字字符强制转换成float类型 ,是将ASCII码进行转换,所以要得到对应数字应减去48
                        //System.out.println(x-48.0f);
                        //System.out.println(x);     
                    }
                    else
                    {
                        int j = i + 1;
                        int start = i;
                        int end = j;
                        boolean f = true;
                        while(f)
                        {
                            if(s[j+1]=='+'||s[j+1]=='-'||s[j+1]=='*'||s[j+1]=='/'||s[j+1]==')')
                            {
                                end = j+1;
                                f = false;
                            }
                            else
                            {
                                j = j + 1;
                            }    
                        }
                        String s2 = str.substring(start,end);//取出star,end下标记录的字符串
                        //System.out.println(s2);
                        float x = Float.parseFloat(s2);// 将字符串强制转换成Float类型
                        q1.push(x);
                        //System.out.println(x);
                        i = end-1;//把end的值赋值给i,让i从下一位开始继续循环
                        //System.out.println(x);
                                                
                    }
                                                
                }                
                /*else if(data=='0'||data=='1'||data=='2'||data=='3'||data=='4'||data=='5'||data=='6'||data=='7'||data=='8'||data=='9')
                {
                
                    if(s[i+1]=='+'||s[i+1]=='-'||s[i+1]=='*'||s[i+1]=='/'||s[i+1]==')')
                    {    
                        int star;
                        int end = 0;
                        star = i;
                        int j;
                        j = i + 1;
                        for(j=i+1;j<i+11;j++)//i+11 是因为规定输入的数字不超过10为,j从i+1开始循环循环不超过10次
                        { 
                            boolean found = false;
                            if(s[j]=='+'||s[j]=='-'||s[j]=='*'||s[j]=='/'||s[j]=='('||s[j]==')')
                            {
                                end = j;
                                found = true;
                                j = 11;//退出循环
                                
                            }
                            else
                            {
                                found = false;
                            }     
                        }
                    
                    String s2 = str.substring(star,end);//取出star,end下标记录的字符串
                    float x = Float.parseFloat(s2);// 将字符串强制转换成Float类型
                    q1.push(x);
                    i = end;//把end的值赋值给i,让i从下一位开始继续循环
                    System.out.println("多个数字");
                    }
                else
                    {
                        float x = s[i];
                        q1.push(x);
                        System.out.println("单个数字");
                    }
                }*/
                        
//处理符号    
                else if(s[i]=='+')
                {
                    q1.push(-1.00f);
                        
                }
                else if(s[i]=='-')
                {                    
                    q1.push(-2.00f);
                    //System.out.println("-");    
                }
                
                else if(s[i]=='*')
                {
                    q1.push(-3.00f);
                    //System.out.println("*");        
                }
                else if(s[i]=='/')
                {
                    q1.push(-4.00f);
                    //System.out.println("/");        
                }        
                else if(s[i]==')')
                {
                    q1.push(-6.00f);
                    //System.out.println(")");        
                }
                //System.out.println("符号");
                
            /*    else if(s[i]=='0'||s[i]=='1'||s[i]=='2'||s[i]=='3'||s[i]=='4'||s[i]=='5'||s[i]=='6'||s[i]=='7'||s[i]=='8'||s[i]=='9')
                {
                    float x = s[i];
                    q1.push(x);    
                }*/
                                                
            
        //}    
        
    }
                boolean e = q1.empty();
                //System.out.println(e);
                e = true;
                int count = 0;
                while(e)
                {                            
                    //System.out.println(q1.pop());
                    float h;
                    h = q1.pop();
                    q2.push(h);
                    if(q1.empty()==false)
                    {
                        e = true;
                    }
                    else
                    {
                        e = false;
                    }
                    count = count + 1;
                }
                //System.out.println(count);
                for(i=0;i<count;i++)
                {
                    System.out.print(" "+q2.pop());
                    /*if(q2.pop()==-1.00000000f)
                        System.out.print(" +");
                    else if(q2.pop()==-2.00000000f)
                        System.out.print(" -");
                    else if(q2.pop()==-3.00000000f)
                        System.out.print(" *");
                    else if(q2.pop()==-4.00000000f)
                        System.out.print(" /");
                    else if(q2.pop()==-5.00000000f)
                        System.out.print(" (");    
                    else if(q2.pop()==-6.00000000f)
                        System.out.print(" )");
                    else
                        System.out.print(" "+q2.pop());        */            
                }
                

              //System.out.println(q1.pop());
              
              
                  
            
    }
}


这个程序很奇怪,报错是
JISUAN.java:7: 类型 Stack 不带有参数
                Stack<Float> q1 = new Stack<Float>();
                     ^
JISUAN.java:7: 类型 Stack 不带有参数
                Stack<Float> q1 = new Stack<Float>();
                                           ^
JISUAN.java:8: 类型 Stack 不带有参数
                Stack<Float> q2 = new Stack<Float>();
                     ^
JISUAN.java:8: 类型 Stack 不带有参数
                Stack<Float> q2 = new Stack<Float>();

奇怪是在我曾经编译这个程序编译就能够通过,但是现在就不行了,而在我同学的电脑上又能通过,换台电脑又不行了,我查过语法,这里并不应该报错,请大家帮我看看怎样做才行

回复列表 (共1个回复)

沙发

是jre的问题,模版是jre1.5+才支持的。

我来回复

您尚未登录,请登录后再回复。点此登录或注册