gpt4 book ai didi

Java JOptionPane 与 LIFO 堆栈

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:09 27 4
gpt4 key购买 nike

我正在学习 Java 类(class),现在我陷入了一个可能非常明显和清晰的问题,但我在互联网上找不到任何答案,所以我决定来这里亲自问你们。

所以.. 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.
}

最佳答案

您将在内存中构建字符串,然后将其用作 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, ....);

关于Java JOptionPane 与 LIFO 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33699535/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com