2015-11-13 71 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 –