区块链技术博客
www.b2bchain.cn

标题: 求各路大神帮帮忙,不知道为什么程序一直显示越界?

D0b2wT.gif

b2bchain.cn区块链技术社区提供第9714篇技术文章标题: 求各路大神帮帮忙,不知道为什么程序一直显示越界?
问题解答:

100行,是哪一行

求各路大神帮帮忙,不知道为什么程序一直显示越界?

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)

标题: 求各路大神帮帮忙,不知道为什么程序一直显示越界? 由www.b2bchain.cn 提供
文章整理自网络,只为个人学习与分享使用
链接地址https://www.b2bchain.cn/?p=9714

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » 标题: 求各路大神帮帮忙,不知道为什么程序一直显示越界?
分享到: 更多 (0)
D0b2wT.gif

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们