2 sinat 33415638 sinat_33415638 于 2016.01.28 15:47 提问

小白求助,为什么这段代码的case“2”未执行

int j = 1;
int i = Integer.parseInt(source.getProperty("表单数据从第几行开始")) - 1;
int lastnum = getNum(source.getProperty("fileName"));
for (int k = 0; k < conditions.length; k++) {
System.out.println(conditions[k]);
switch (conditions[k]) {
case "1":
for (; i < lastnum; i++) {
XSSFRow readrow = readsheet.getRow(i);// 获取输入行
Process p = new ProcessRow();
if (p.panduan1(readrow)) {
System.out.println("get1");
XSSFSheet sheet = sc.getSheetAt(0);
XSSFRow outputrow = sheet.createRow(j++);
p.process1(readrow, outputrow);
p.process2(readrow, outputrow);
p.process3(readrow, outputrow);
p.process4(readrow, outputrow);
}
}
case "2":
for (; i < lastnum; i++) {
XSSFRow readrow = readsheet.getRow(i);// 获取输入行
Process p = new ProcessRow();
System.out.println("get");
if (p.panduan2(readrow)) {
System.out.println("get2");
XSSFSheet sheet = sc.getSheetAt(0);
XSSFRow outputrow = sheet.createRow(j++);
p.process2(readrow, outputrow);
}
}
default:
break;
}

        输出结果
        1

get1
get1
2
3
4
输入完毕

为什么case 2没执行

7个回答

galiniur0u
galiniur0u   2016.01.28 15:58
已采纳

i值没有还原,导致执行完case 1时i > lastnum,当执行到case 2的时候i依旧大于lastnum,for循环直接跳出,因此为执行。
你的程序怎么case之后不跟break?case之后不跟break就会导致进入switch之后所有的case均执行一遍。

sinat_33415638
sinat_33415638 谢谢
接近 2 年之前 回复
sinat_33415638
sinat_33415638 谢谢
接近 2 年之前 回复
rui888
rui888   Ds   Rxr 2016.01.28 16:04

调试吧 case "1": 后面不加break ?

WinsenJiansbomber
WinsenJiansbomber   2016.01.29 08:38

排版太乱了,这不StackEdit嘛,可以试试插入代码功能啊

//例如这样了
WinsenJiansbomber
WinsenJiansbomber   2016.01.29 08:41

代码括号都不完整,看我整理的:

 int j = 1;
int i = Integer.parseInt(source.getProperty("表单数据从第几行开始")) - 1;
int lastnum = getNum(source.getProperty("fileName"));
for (int k = 0; k < conditions.length; k++) {
    System.out.println(conditions[k]);
    switch (conditions[k]) {
        case "1":
        for (; i < lastnum; i++) {
            XSSFRow readrow = readsheet.getRow(i);// 获取输入行
            Process p = new ProcessRow();
            if (p.panduan1(readrow)) {
                System.out.println("get1");
                XSSFSheet sheet = sc.getSheetAt(0);
                XSSFRow outputrow = sheet.createRow(j++);
                p.process1(readrow, outputrow);
                p.process2(readrow, outputrow);
                p.process3(readrow, outputrow);
                p.process4(readrow, outputrow);
            }
        }
        case "2":
        for (; i < lastnum; i++) {
            XSSFRow readrow = readsheet.getRow(i);// 获取输入行
            Process p = new ProcessRow();
            System.out.println("get");
            if (p.panduan2(readrow)) {
                System.out.println("get2");
                XSSFSheet sheet = sc.getSheetAt(0);
                XSSFRow outputrow = sheet.createRow(j++);
                p.process2(readrow, outputrow);
            }
        }
        default:
        break;
    }
WinsenJiansbomber
WinsenJiansbomber   2016.01.29 08:42

CSDN你色一点试试:

 int j = 1;
int i = Integer.parseInt(source.getProperty("表单数据从第几行开始")) - 1;
int lastnum = getNum(source.getProperty("fileName"));
for (int k = 0; k < conditions.length; k++) {
    System.out.println(conditions[k]);
    switch (conditions[k]) {
        case "1":
        for (; i < lastnum; i++) {
            XSSFRow readrow = readsheet.getRow(i);// 获取输入行
            Process p = new ProcessRow();
            if (p.panduan1(readrow)) {
                System.out.println("get1");
                XSSFSheet sheet = sc.getSheetAt(0);
                XSSFRow outputrow = sheet.createRow(j++);
                p.process1(readrow, outputrow);
                p.process2(readrow, outputrow);
                p.process3(readrow, outputrow);
                p.process4(readrow, outputrow);
            }
        }
        case "2":
        for (; i < lastnum; i++) {
            XSSFRow readrow = readsheet.getRow(i);// 获取输入行
            Process p = new ProcessRow();
            System.out.println("get");
            if (p.panduan2(readrow)) {
                System.out.println("get2");
                XSSFSheet sheet = sc.getSheetAt(0);
                XSSFRow outputrow = sheet.createRow(j++);
                p.process2(readrow, outputrow);
            }
        }
        default:
        break;
    }
WinsenJiansbomber
WinsenJiansbomber   2016.01.29 08:42

不行啊,这编辑器,不加彩的:

 int j = 1;
int i = Integer.parseInt(source.getProperty("表单数据从第几行开始")) - 1;
int lastnum = getNum(source.getProperty("fileName"));
for (int k = 0; k < conditions.length; k++) {
    System.out.println(conditions[k]);
    switch (conditions[k]) {
        case "1":
        for (; i < lastnum; i++) {
            XSSFRow readrow = readsheet.getRow(i);// 获取输入行
            Process p = new ProcessRow();
            if (p.panduan1(readrow)) {
                System.out.println("get1");
                XSSFSheet sheet = sc.getSheetAt(0);
                XSSFRow outputrow = sheet.createRow(j++);
                p.process1(readrow, outputrow);
                p.process2(readrow, outputrow);
                p.process3(readrow, outputrow);
                p.process4(readrow, outputrow);
            }
        }
        case "2":
        for (; i < lastnum; i++) {
            XSSFRow readrow = readsheet.getRow(i);// 获取输入行
            Process p = new ProcessRow();
            System.out.println("get");
            if (p.panduan2(readrow)) {
                System.out.println("get2");
                XSSFSheet sheet = sc.getSheetAt(0);
                XSSFRow outputrow = sheet.createRow(j++);
                p.process2(readrow, outputrow);
            }
        }
        default:
        break;
    }
WinsenJiansbomber
WinsenJiansbomber   2016.01.29 08:44

还是不彩:

         for (; i < lastnum; i++) {
            XSSFRow readrow = readsheet.getRow(i);// 获取输入行
            Process p = new ProcessRow();
            if (p.panduan1(readrow)) {
                System.out.println("get1");
                XSSFSheet sheet = sc.getSheetAt(0);
                XSSFRow outputrow = sheet.createRow(j++);
                p.process1(readrow, outputrow);
                p.process2(readrow, outputrow);
                p.process3(readrow, outputrow);
                p.process4(readrow, outputrow);
            }
        }

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!