繁体   English   中英

JOptionPanel上不可预测的Java程序运行时错误

[英]Unpredictable java program runtime error on JOptionPanel

我编写了一个代码,其目的是在给定总体的情况下创建样本。 例如,如果我给四个元素(0、1、2、3)填充,并且样本由1个元素组成,则输出将为0 1 2 3在JOptionPane上打印的每个值。

如果样本是由两个元素组成,则输出为:00 01 02 03 10 11 12 13 20 21 23 30 31 32 33

如果Saple由三个元素组成,则输出为:000 001 002 003 010 010 011 012 013,依此类推

我递归地编写了该程序,以便可以在每个样本的维度上运行。

该程序应在JOptionPane上打印值(包含在矢量中)时停止,而我不明白其动机。 这是代码:

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


public class Combo {

/**
 * Function that recursively prints samples
 * @param n size of samples
 * @param popolazione elements of the population
 * @param combinazione the vector that contains the uncomplete sample
 */
public static void stampa(int n, Vector<Integer> popolazione, Vector<Integer> combinazione){
    // exit condition, when n equals 0 the function doesn't self-calls
    if(n==0) JOptionPane.showMessageDialog(null, combinazione);

    // for every element of the population the function adds this element
    // at the previous combination
    for(int x = 0; x<popolazione.size();x++){
        Vector<Integer> aggiunta = new Vector<Integer>(combinazione);
        aggiunta.add(x);
        stampa(n-1, popolazione, aggiunta);
    }
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    // creation a vector population with 4 elements
    Vector<Integer> popolazione = new Vector<Integer>();
    for(int x = 0; x < 4; x++) popolazione.add(x);

    // creation of samples
    stampa(1, popolazione, new Vector<Integer>());
}
}

我的错误在哪里?

在您的方法stampa() ,我相信您想更改此设置

if(n==0) JOptionPane.showMessageDialog(null, combinazione);

对此

if (n < 1) {
  JOptionPane.showMessageDialog(null,
      combinazione.get(n));
  return;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM