JOptionPane 不打印 for 循环变量的方法

问题描述

我有一个叫做“cambiar”的方法,我从一个叫做“articulos”的数组接收数据,然后我把这些数据放在我的“datos”变量中。 但输出只是打印如下内容

选择您要更改的文章 xx(我用这个xx词作为参考)

我的代码没有打印“datos”变量,我不知道为什么。

'[A-Za-z0-9!#$%&'*+/=?^_

解决方法

我看到一些可能是问题的行:

  1. articulos.get(art); 不执行任何操作,因此我建议将其删除。
  2. datos = "Articulo: " + nombre + ",Cantidad:" + cantidad + "\n"; 行中有 2 个问题。 第一个是我假设你想显示所有 exsts 文章,所以你需要用 = 替换 += 以将所有字符串添加到一个。 第二个是您在 \n 中的 JOptionPane 中使用,这将不起作用。 在 JOptionPane 中,您可以仅使用 HTML 而非 java String 语法来设置消息样式。 除了替换 "<html>" 中的所有 "</html>" 之外,您还可以通过在您的消息之前添加 "\n" 来实现这一点(对于更可靠的代码,"</br>" 在此之后)。 例如:
public ArrayList<Item> cambiar() {
        
    String datos = "<br/>";
    int cantidad;
    String nombre;
    for (int i = 0; i < articulos.size(); i++) {
        cantidad = articulos.get(i).getCantidad();
        nombre = articulos.get(i).getP().getNombre();
        datos += "Articulo: " + nombre + ",Cantidad:" + cantidad + "<br/>";
    }
    int art = Integer.parseInt(JOptionPane.showInputDialog(null,"<html>Select which article you would like to change<br/>"+ datos+"xx</html>"));
    articulos.get(art);
    articulos.get(art).getCantidad();
    int cuan = Integer.parseInt(JOptionPane.showInputDialog(null,"How many would you like buy?"));
    articulos.get(art).setCantidad(cuan);
    return articulos;
}
,

好像你的 articulos 列表是空的,for 循环不是事件开始。

我不确定你想用这个方法/消息做什么,即使你设法修复了你的“articulos”列表,你的“datos”变量也只会得到列表的最后一个元素。要么使用 += 复合赋值运算符组合您的数据,要么将其余代码移动到 for 循环中(而不是使用 +=),以便在每个元素上弹出对话框。