2015-11-13 72 views
1

我在java課程,現在我陷入了一個問題,這可能是非常明顯,清楚,但我無法找到任何答案在互聯網上,所以我決定來這裏,親自問你們。Java JOptionPane與LIFO棧的

所以.. JOpitionPane顯示LIFO(後進先出)堆棧。 在我的代碼中,我使用System.out.println作爲示例來顯示我想要的操作。我需要做的是將它顯示在JOptionPane.showMessageDialog框中。 我不知道怎麼弄出來,創建一個數組來堆疊你想顯示的數量是我的猜測,但我不知道如何從這裏前進。

非常感謝誰能回答我的問題。

這是我對這個問題的簡化代碼文本。

import java.util.Stack; 
import javax.swing.JOptionPane; 

public class Test1 { 
public static void main(String args[]) { 
    new Test1(); 

} 

public Test1() { 

    boolean status = false; 

    Stack<String> lifo = new Stack<>(); 

    while (!status) { 
     String s = (JOptionPane.showInputDialog("Write something")); 

     if (s == null) { 

      status = true; 

     } else { 
      lifo.add(s); 
     } 

    } 
    if (status == true) { 
     Double num = Double.parseDouble(JOptionPane.showInputDialog("How many of latest Input would you like to see?")); 
     for (int i = 0; i < num; i++) { 
      System.out.println(lifo.pop()); //Here is where i would want 
      System.out.print(',');   //JOptionPane.showMessageDialog instead. 
     } 

回答

0

您會在內存中構建字符串,然後將其用作showMessageDialog的消息。例如:

String msg = ""; 
for (int i = 0; i < num; i++) { 
    if (i > 0) 
     msg += ","; // could replace this with a newline to have the numbers stacked 
    msg += lifo.pop(); 
} 
JOptionPane.showMessageDialog("title", msg, ....); 
+0

ahh是的謝謝你,這使得它在代碼中少了一個問題:P –