gpt4 book ai didi

java - JOptionPanel 上出现不可预测的 java 程序运行时错误

转载 作者:行者123 更新时间:2023-12-01 13:48:29 24 4
gpt4 key购买 nike

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

如果样本由两个元素组成,则输出将为:000102031011121320212330313233

如果树苗由三个元素组成,则输出将为:000001002003010011012013等

我递归地编写了这个程序,以便它可以针对每个样本的维度运行。

程序在应该在 JOptionPane 上打印值(包含在 vector 中)时停止,我无法理解其动机。这是代码:

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;
}

关于java - JOptionPanel 上出现不可预测的 java 程序运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20160263/

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