【问题标题】:switch case in implementation of stack using array in java在java中使用数组实现堆栈的switch case
【发布时间】:2016-11-25 03:05:00
【问题描述】:
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代码来实现堆栈。但是一旦我选择了任何选项,只有那个方法被执行并且程序结束。我希望程序在执行当前方法后让我输入另一个选项。我该怎么办?

【问题讨论】:

  • 你能发布一个测试用例 i/o 的例子吗?
  • 我不认为该程序正在结束,我认为它永远在旋转。考虑在 do-while 循环中移动 opt=s.nextInt();

标签: java arrays stack switch-statement


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您可以通过以下方式修改您的 main 方法: public static 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; 
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 2021-10-22
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多