小编Vla*_* K.的帖子

Java数组索引超出范围?

有人可以帮我解决下面发布的错误吗?该程序运行,但在调用显示捐赠时,我收到关于我的主要的错误.我怎么能解决这个问题呢?是否与语法错误有关?这是我第一次使用数组,所以我欢迎有关良好习惯的反馈.谢谢!

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
        at donations.processDonations(donations.java:78)
        at donations.main(donations.java:33)

    import java.util.Scanner; //needed for input

    public class donations {
        static double[] cashDonations = new double[6]; // stores the cash donations
                                                        // from 6 sites
        static double[] lbsFood = new double[6]; // stores the pounds of food
                                                    // donated from 6 sites
        static String[] siteName = new String[6]; // stores the names of the 6 sites
        static String bestSiteCash = " "; // stores the site name with the highest …
Run Code Online (Sandbox Code Playgroud)

java arrays runtime-error

2
推荐指数
1
解决办法
625
查看次数

用于表达式求值的 Dijkstra 两栈算法

我正在读一本关于算法和数据结构的,并尝试遵循其中的示例。我尝试实现的是 Dijkstra 的用于表达式求值的两栈算法。它以字符串形式输入( 1 + 2 ) * 3,然后计算表达式。我的代码可以编译,但不会产生正确的输出。

上述表达式的输出是:

3.0
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

public class Eval {
    public static void main(String[] args) {
        String s = "( 1 + 2 ) * 3";
        evaluateAndPrintResult(s);
    }
    public static void evaluateAndPrintResult(String s)
    {
        String[] str = s.split("\\s+");
        Queue<String> q = new LinkedList<String>();
        for(String ss : str)
            q.add(ss);
        Stack<String> ops = new Stack<String>();
        Stack<Double> vals = new Stack<Double>();
        while (!q.isEmpty())
        { // Read token, push if operator.
            String token = …
Run Code Online (Sandbox Code Playgroud)

java algorithm stack

1
推荐指数
1
解决办法
8163
查看次数

标签 统计

java ×2

algorithm ×1

arrays ×1

runtime-error ×1

stack ×1