0

我在下面的代码中遇到了非常困难的事情......我试图让苹果、橙子和香蕉显示为推、拉和窥视。但是,我不断得到0

   import java.util.*;

   public class GenericStackApp {

       private static int apple;
       private static int banana;
       private static int orange;

       static void showpush(Stack st, int apple) 
       {
            st.push(new Integer(apple + banana + orange));
            System.out.println("push(" + apple + banana + orange + ")");
            System.out.println("stack: " + apple + banana + orange );
       }

       static void showpop(Stack st) 
       {
            System.out.print("pop -> ");
            Integer a = (Integer) st.pop();
            System.out.println(a);
            System.out.println("stack: " + st);
       }

       public static void main(String args[]) 
       {
           Stack st = new Stack();
           System.out.println("stack: " + st);
           showpush(st, apple);
           showpush(st, banana);
           showpush(st, orange);
           showpop(st);
           showpop(st);
           showpop(st);
           try 
           {
               showpop(st);
           } catch (EmptyStackException e) 
           {
               System.out.println("3 items in the stack");
           }
        }
    }

先感谢您。

4

1 回答 1

2

您需要像这样初始化变量 apple、banana 和 orange:

     private static int apple = 1;
     private static int banana = 2;
     private static int orange = 3;
于 2013-08-09T02:26:51.870 回答