在java中使用数组实现堆栈的switch case

时间:2016-07-21 13:29:19

标签: java arrays stack switch-statement

import java.io.*;
import java.util.*;

public class Stack1{

static final int MAX=100;
int top=-1;

int[] stack=new int[MAX];

public static void main(String args[])
{
    Stack1 s1=new Stack1();
        int opt, val;
        System.out.println("1. PUSH ");
        System.out.println("2. POP ");
        System.out.println("3. PEEP ");
        System.out.println("4. DISPLAY STACK ");
        System.out.println("5. EXIT ");
        System.out.println("\n Enter Your Option: ");
        Scanner s=new Scanner(System.in);
        opt=s.nextInt();
    do{
        switch(opt)
        {
            case 1: System.out.println("Enter the value to be added to the stack: ");
                    val=s.nextInt();
                    s1.push(val);
                    break;

            case 2: s1.pop();
                    break;
                /*
            case 3: s1.peep();
                    break; */

            case 4: s1.display();
                    break;

        }
    }while(opt!=5);

}

public void push(int val)
{
    if(top==MAX-1)
    {
        System.out.println("Stack is FULL!");
    }
    else
    {
        top++;
        stack[top]=val;
        System.out.println("Element added to the stack is: "+val);
        display();
    }
}

public void pop()
{
    int x;
    if(top==-1)
    {
        System.out.println("Stack is EMPTY!");
    }
    else
    {
        x=stack[top];
        System.out.println("The element deleted from the stack is: "+x);
        top--;
        display();
    }
}

public void peep()
{
    int n;
    n=stack[top];
    System.out.println("The value at the top of the stack is: "+n);
}

public void display()
{
    int i;
    if(top==-1)
    System.out.println("STACK IS EMPTY!");
    else
    {
        for(i=0; i<=top; i++)
        System.out.println("The elements in the stack are: "+stack[i]);
    }

}

}

我写了这个java代码来实现堆栈。但是一旦我选择了任何选项,只有该方法被执行并且程序结束。我希望程序在执行当前方法后让我输入另一个选项。我该怎么办?

2 个答案:

答案 0 :(得分:1)

正如@LordWilmore在他的评论中指出的那样,opt值只会被设置一次,导致程序在相应的情况下永远旋转(除非值为5)。移动opt = s.nextInt();内部循环将解决问题。

do {
    System.out.println("Enter Your Option: ");
    opt = s.nextInt();
    switch(opt) {
        //...
    }
} while (opt != 5);

答案 1 :(得分:0)

您可以通过以下方式修改main方法: 公共静态void main(String args []){

    int option;
    Scanner sc = new Scanner(System.in);
    MyStack1 s = new MyStack1();

    while(true){

        System.out.println("Enter the choice You want to perform on the stack: ");
        System.out.println(" 1. push \n 2. Pop \n 3. Display \n 4. peep \n 5. Exit");
        System.out.println("Enter your option: ");
        option = sc.nextInt();

        switch(option){

            case 1: System.out.println("Enter the element you want to push into the stack: ");
                    s.push(sc.nextInt());
                    break;

            case 2: s.pop();
                    break;

            case 3: System.out.println("Displaying the stack contents: ");
                    s.display();
                    break;

            case 4: System.out.println("The top element in the stack is: ");
                    s.peek();
                    break;

            case 5: System.out.println("You selected Exit!!");
                    break;

            default: System.out.println("Wrong choice!! Please enter a  valid option!!");
                     return; 
        }
    }
}