我正在編寫一個程序,允許用戶在堆棧中輸入正整數(以0結尾)並按相反順序顯示它們。我第一次嘗試打印出棧的元素來測試它第一,但該計劃不打印出來的元素,當我輸入0 這裏是我的程序:Java-數據結構堆棧:從用戶輸入的堆棧打印出整數
import java.util.*;
public class MyClass{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
Stack<Integer> addToStack= new Stack<Integer>();
int num;
System.out.println("Enter the a list of positive integers. Terminate with a 0.");
num= sc.nextInt();
while(num!=0){
addToStack.push(num);
}
System.out.println("Displaying numbers from the stack "+ addToStack);
}
}
'NUM = sc.nextInt();'應該在側循環。 – Satya