繁体   English   中英

为什么switch语句不进入情况2?

[英]Why the switch statement does not enter to the case 2?

感谢您阅读我的问题。 我的编译器没有任何问题,当我运行该程序时,情况1运行良好,但情况2无法运行,当我尝试使用它时,程序返回主菜单。 比您的帮助。

public static void main(String[] args) {
    int opcion=0;
    double[][] m;

    do{

        m=new double[0][0];
        opcion=Integer.parseInt(JOptionPane.showInputDialog(null,"El programa permite:\n1.Ingresar Inventario\n2.Calcular Inventario\n3.Salir"));
        switch(opcion){
            case 1: 


                m=Metodos.LLenarMatriz();
                Metodos.Imprimir(m);

            break;

            case 2:

                double[][] r=Metodos.CalcularInventario(m);
                Metodos.Imprimir(r);
            break;    
        }

    }
    while(opcion!=3);


    }

}

这是两种方法:

public static double[][] CalcularInventario (double[][] x){

    double fac,n=0;
    int filx;
    filx=x.length;
    double [][] c=new double[filx][3];
    for(int i=0;i<x.length;i++){
        for(int j=0;j<x[i].length;j++){
            c[i][j]=x[i][j];
        }
    }
    for(int i=0; i<c.length;i++)
    {
        fac=x[i][0];


        n=fac*c[i][1];
        c[i][2]=n;



    } 

    return c; 
}

public static void Imprimir(double[][] m){

    for(int i=0;i<m.length;i++){
        System.out.print("\n");
        for(int j=0;j<m[i].length;j++){

            System.out.print(m[i][j]+"|");

        }

    }
}

}

这是使用调试器可帮助调试代码的地方。

m=new double[0][0];

所以矩阵是空的

int filx;
filx=x.length;
double [][] c=new double[filx][3];

并且x.length为0,因此c为空。

for(int i=0;i<m.length;i++){
    System.out.print("\n");

m.length仍为0,因此不会打印任何内容。

我建议您在调试器中逐步执行代码,以了解它在什么时候没有达到您的预期。 我还建议您在IDe中使用格式化程序,以使代码更易于阅读。

暂无
暂无

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

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