Hola alguien me ayudaria con mi codigo de combinacion sin repeticion
solo me funciona el caso 2
:V
Solo me crea el primero y el segundo caso. como aria el tercer caso?
package permutacioncombinacion;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Csinrepeticion extends JFrame{
public JButton regresar,info,calcular,reinicio;
public JLabel n,r,resultado,calculado;
public JTextField n1,r1;
public JTextArea area;
public int mayor=-1;
public Csinrepeticion (){
super ();
configuraVentana();
incializarComponentes();
}
public void configuraVentana(){
setTitle(".:Combinacion :.");
setSize(300,480);
setLocationRelativeTo(null);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void incializarComponentes(){
reinicio = new JButton("Reiniciar");
reinicio.setBounds(100,175,100,20);
this.add(reinicio);
reinicio.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
area.setText("");
n1.setText("");
r1.setText("");
calculado.setText("");
}
});
//**************************
area = new JTextArea();
area.setBounds(30,180,260,200);
area.setBackground( Color.black );
area.setEditable(false);
this.add(area);
JScrollPane scroll = new JScrollPane(area);
scroll.setBounds(10, 200, 260, 180);
this.add(scroll);
//************************
n = new JLabel ("Numeros a elegir(n)");
n.setBounds(30,50,120,20);
this.add(n);
n1 = new JTextField ();
n1.setBounds(160,50,120,20);
this.add(n1);
n1.addActionListener(new ActionListener(){
@Override
public void actionPerformed (ActionEvent e){
r1.requestFocus();
}
});
//**************************
r = new JLabel ("Cuantas Formas(r)");
r.setBounds(30,100,120,20);
this.add(r);
r1 = new JTextField ();
r1.setBounds(160,100,120,20);
this.add(r1);
r1.addActionListener(new ActionListener(){
@Override
public void actionPerformed (ActionEvent e){
calcular.requestFocus();
}
});
//**************
resultado = new JLabel ("Combinaciones = ");
resultado.setBounds(30,150,150,20);
this.add(resultado);
calculado = new JLabel ("");
calculado.setBounds(180 , 150 , 120 , 20);
this.add(calculado);
calculado.setVisible(false);
//************************
info = new JButton ("?");
info.setBounds(230,5,50,20);
this.add(info);
info.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
JOptionPane.showMessageDialog(null,"Permutaciones sin repetición de n elementos (de orden n)\n son los distintos grupos de n elementos distintos que se pueden hacer\n de forma que dos grupos se diferencian únicamente\n en el orden de colocación de los elementos");
}
});
regresar = new JButton ("Cerrar");
regresar.setBounds(40,240,100,20);
this.add(regresar);
regresar.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
PermutacionCombinacion k =new PermutacionCombinacion();
k.setVisible(true);
setVisible(false);
}
});
regresar = new JButton ("Cerrar");
regresar.setBounds(40,400,100,20);
this.add(regresar);
regresar.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
PermutacionCombinacion k =new PermutacionCombinacion();
k.setVisible(true);
setVisible(false);
}
});
calcular = new JButton ("Calcular");
calcular.setBounds(160,400,100,20);
this.add(calcular);
calcular.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
area.setText("");
double total=1 ,npr, nfactorial = 1,rfactorial = 1;
int r2;
Double n2;
if ("".equals(n1.getText())){
JOptionPane.showMessageDialog(null,"casiilla 1 \nEscribe un numero ");
return;
}
if ("".equals(r1.getText())){
JOptionPane.showMessageDialog(null,"casee 2 \nEscribe un numero ");
return;
}
try{
n2 = Double.parseDouble(n1.getText());
r2 = Integer.parseInt(r1.getText());
}catch(NumberFormatException x){
JOptionPane.showMessageDialog(null, "Solo se aceptan numeros :C");
return;
}
if(n2>0 && n2 <31){
Double Csinrepeticion = factorial(n2)/(factorial(n2-r2)*factorial(r2));
calculado.setText(""+Csinrepeticion);
calculado.setVisible(true);
int h =0;
int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
int mayor = 0,suma=0;
switch (r2){
case 1:
for(int i = 0 ; i< n2 ;i++){
area.setEditable(true);
area.setForeground( Color.red );
area.append((h)+".- "+arr+"\n");
mayor =arr;
area.setEditable(false);
}
break;
case 2:
int s=1;
for(int i = 0; i < n2;i++)
for(int j =s; j < n2;j++){
if(arr!=arr[j]){
h++;
area.setEditable(true);
area.setForeground( Color.blue);
area.append((h)+".- "+arr +","+ arr[j]+"\n");
mayor=suma;
area.setEditable(false);
if(j==(n2-1)) {
s=s+1;
}
}
}
break;
case 3:
int a =1 ,b =2;
for(int i = 0; i < n2;i++){
for(int j = a; j < n2;j++){
b=0;
for(int k = b; k < n2;k++){
h++;
area.setEditable(true);
area.setForeground( Color.pink );
area.append((h)+".- "+arr +","+ arr[j] +","+ arr[k] + "\n");
area.setEditable(false);
if(k == (n2-1))
a =a+1;
}
/*if(j == (n2-1))
a++;*/
}
}
break;
case 4:
for(int i = 0; i < n2;i++){
for(int j = 0; j < n2; j++){
for(int k = 0; k < n2; k++){
for(int l = 0; l < n2 ; l++){
if(arr!= arr[j] && arr[j]!=arr[k] && arr[k]!= arr[l] ){
if(arr !=arr[l]){
if(arr !=arr[k]){
if(arr[j]!=arr[l]){
h++;
area.setEditable(true);
area.setForeground( Color.green );
area.append((h)+".- "+arr +","+ arr[j] +","+ arr[k] +","+arr[l]+"\n");
area.setEditable(false);}}
}}}
}
}
}
break;
default:
JOptionPane.showMessageDialog(null,"Casilla 2\nIngresa Numeros en el rango[1,4]");
}
}else
JOptionPane.showMessageDialog(null,"casilla 1 \nIngresa numeros en el rango[1,30]");
}
});
}
public static double factorial(double x){
double nfactorial = 1;
for( int i = 0 ; i < x; i++){
nfactorial = nfactorial * (x-i);
}
return (nfactorial);
}
public static void main(String[] args) {
Csinrepeticion p = new Csinrepeticion();
p.setVisible(true);
}
}
solo me funciona el caso 2
:V
Solo me crea el primero y el segundo caso. como aria el tercer caso?
package permutacioncombinacion;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Csinrepeticion extends JFrame{
public JButton regresar,info,calcular,reinicio;
public JLabel n,r,resultado,calculado;
public JTextField n1,r1;
public JTextArea area;
public int mayor=-1;
public Csinrepeticion (){
super ();
configuraVentana();
incializarComponentes();
}
public void configuraVentana(){
setTitle(".:Combinacion :.");
setSize(300,480);
setLocationRelativeTo(null);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void incializarComponentes(){
reinicio = new JButton("Reiniciar");
reinicio.setBounds(100,175,100,20);
this.add(reinicio);
reinicio.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
area.setText("");
n1.setText("");
r1.setText("");
calculado.setText("");
}
});
//**************************
area = new JTextArea();
area.setBounds(30,180,260,200);
area.setBackground( Color.black );
area.setEditable(false);
this.add(area);
JScrollPane scroll = new JScrollPane(area);
scroll.setBounds(10, 200, 260, 180);
this.add(scroll);
//************************
n = new JLabel ("Numeros a elegir(n)");
n.setBounds(30,50,120,20);
this.add(n);
n1 = new JTextField ();
n1.setBounds(160,50,120,20);
this.add(n1);
n1.addActionListener(new ActionListener(){
@Override
public void actionPerformed (ActionEvent e){
r1.requestFocus();
}
});
//**************************
r = new JLabel ("Cuantas Formas(r)");
r.setBounds(30,100,120,20);
this.add(r);
r1 = new JTextField ();
r1.setBounds(160,100,120,20);
this.add(r1);
r1.addActionListener(new ActionListener(){
@Override
public void actionPerformed (ActionEvent e){
calcular.requestFocus();
}
});
//**************
resultado = new JLabel ("Combinaciones = ");
resultado.setBounds(30,150,150,20);
this.add(resultado);
calculado = new JLabel ("");
calculado.setBounds(180 , 150 , 120 , 20);
this.add(calculado);
calculado.setVisible(false);
//************************
info = new JButton ("?");
info.setBounds(230,5,50,20);
this.add(info);
info.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
JOptionPane.showMessageDialog(null,"Permutaciones sin repetición de n elementos (de orden n)\n son los distintos grupos de n elementos distintos que se pueden hacer\n de forma que dos grupos se diferencian únicamente\n en el orden de colocación de los elementos");
}
});
regresar = new JButton ("Cerrar");
regresar.setBounds(40,240,100,20);
this.add(regresar);
regresar.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
PermutacionCombinacion k =new PermutacionCombinacion();
k.setVisible(true);
setVisible(false);
}
});
regresar = new JButton ("Cerrar");
regresar.setBounds(40,400,100,20);
this.add(regresar);
regresar.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
PermutacionCombinacion k =new PermutacionCombinacion();
k.setVisible(true);
setVisible(false);
}
});
calcular = new JButton ("Calcular");
calcular.setBounds(160,400,100,20);
this.add(calcular);
calcular.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
area.setText("");
double total=1 ,npr, nfactorial = 1,rfactorial = 1;
int r2;
Double n2;
if ("".equals(n1.getText())){
JOptionPane.showMessageDialog(null,"casiilla 1 \nEscribe un numero ");
return;
}
if ("".equals(r1.getText())){
JOptionPane.showMessageDialog(null,"casee 2 \nEscribe un numero ");
return;
}
try{
n2 = Double.parseDouble(n1.getText());
r2 = Integer.parseInt(r1.getText());
}catch(NumberFormatException x){
JOptionPane.showMessageDialog(null, "Solo se aceptan numeros :C");
return;
}
if(n2>0 && n2 <31){
Double Csinrepeticion = factorial(n2)/(factorial(n2-r2)*factorial(r2));
calculado.setText(""+Csinrepeticion);
calculado.setVisible(true);
int h =0;
int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
int mayor = 0,suma=0;
switch (r2){
case 1:
for(int i = 0 ; i< n2 ;i++){
area.setEditable(true);
area.setForeground( Color.red );
area.append((h)+".- "+arr+"\n");
mayor =arr;
area.setEditable(false);
}
break;
case 2:
int s=1;
for(int i = 0; i < n2;i++)
for(int j =s; j < n2;j++){
if(arr!=arr[j]){
h++;
area.setEditable(true);
area.setForeground( Color.blue);
area.append((h)+".- "+arr +","+ arr[j]+"\n");
mayor=suma;
area.setEditable(false);
if(j==(n2-1)) {
s=s+1;
}
}
}
break;
case 3:
int a =1 ,b =2;
for(int i = 0; i < n2;i++){
for(int j = a; j < n2;j++){
b=0;
for(int k = b; k < n2;k++){
h++;
area.setEditable(true);
area.setForeground( Color.pink );
area.append((h)+".- "+arr +","+ arr[j] +","+ arr[k] + "\n");
area.setEditable(false);
if(k == (n2-1))
a =a+1;
}
/*if(j == (n2-1))
a++;*/
}
}
break;
case 4:
for(int i = 0; i < n2;i++){
for(int j = 0; j < n2; j++){
for(int k = 0; k < n2; k++){
for(int l = 0; l < n2 ; l++){
if(arr!= arr[j] && arr[j]!=arr[k] && arr[k]!= arr[l] ){
if(arr !=arr[l]){
if(arr !=arr[k]){
if(arr[j]!=arr[l]){
h++;
area.setEditable(true);
area.setForeground( Color.green );
area.append((h)+".- "+arr +","+ arr[j] +","+ arr[k] +","+arr[l]+"\n");
area.setEditable(false);}}
}}}
}
}
}
break;
default:
JOptionPane.showMessageDialog(null,"Casilla 2\nIngresa Numeros en el rango[1,4]");
}
}else
JOptionPane.showMessageDialog(null,"casilla 1 \nIngresa numeros en el rango[1,30]");
}
});
}
public static double factorial(double x){
double nfactorial = 1;
for( int i = 0 ; i < x; i++){
nfactorial = nfactorial * (x-i);
}
return (nfactorial);
}
public static void main(String[] args) {
Csinrepeticion p = new Csinrepeticion();
p.setVisible(true);
}
}