| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1174 人关注过本帖
标题:求各路大神帮帮忙,不知道为什么程序一直显示越界?
只看楼主 加入收藏
slamdunkbh
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-11-12
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
求各路大神帮帮忙,不知道为什么程序一直显示越界?
package shiyan;
import java.util.Scanner;
public class qiuzhi {
    public static void main(String[] args) {
        String a=new String();
        int x=0;
        stack<Character> stack=new stack<Character>();
        System.out.println("请输入:");
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        String[] n=new String[100];
        for(int i=0;i<str.length();i++){
            if(((int)str.charAt(i)>=48 && (int)str.charAt(i)<=57)||str.charAt(i)=='.'){
                a=a+Character.toString(str.charAt(i));
                    if(i==str.length()-1) {
                        n[x]=a;
                    }
                    else {
                        if(((int)str.charAt(i)<=48 && (int)str.charAt(i)>=57)||str.charAt(i)!='.'){
                            n[x]=a;
                            a="";
                            x=x+1;
                        }
                        else {
                            continue;
                        }
                    }
            }
            else{
                if(stack.isempty()){
                    stack.push(str.charAt(i));
                }
                else if(str.charAt(i)=='('){
                    stack.push(str.charAt(i));
                }
                else if(str.charAt(i)==')'){
                    while(stack.peek()!='('){
                        n[x]=Character.toString(stack.peek());
                        x=x+1;
                    }
                    stack.pop();
                }
                else{
                    while(panduan.getValue(str.charAt(i))<=panduan.getValue(stack.peek())){
                        n[x]=Character.toString(stack.peek());
                        x=x+1;
                    }
                    stack.push(str.charAt(i));
                }
            }
        }
        while(!stack.isempty()){
            n[x]=Character.toString(stack.peek());
            x=x+1;
        }
        for(int i=0;i<x;i++) {
            System.out.println(n[i]);
        }


    }

}
class panduan{
    public static int getValue(char b){
        int result=0;
        switch(b){
        case'+':
            result=0;
            break;
        case'-':
            result=0;
            break;
        case'*':
            result=0;
            break;
        case'/':
            result=0;
            break;
        }
        return result;
    }
}


输出结果
请输入:
1+1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
    at shiyan.qiuzhi.main(qiuzhi.java:53)
搜索更多相关主题的帖子: str stack result int Character 
2020-11-12 11:29
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:20 
100行,是哪一行

剑栈风樯各苦辛,别时冰雪到时春
2020-11-12 18:41
快速回复:求各路大神帮帮忙,不知道为什么程序一直显示越界?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017244 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved