简体   繁体   English

Java-将else语句放入循环中

[英]Java - Put else statement in looping for

I want to make the handling of which if the data I input is not found. 如果找不到我输入的数据,我想进行处理。 However, when the data is not found, the program will print a message else as much repetition. 但是,当找不到数据时,程序将打印一条消息,否则将重复多次。 Where should I put the else statement correctly? 我应该在哪里正确放置else语句?

public void Data(String x){
    for (int i = 1; i < 6; i++) {
        if (x.equalsIgnoreCase(table1[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table1[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table2[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table2[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table3[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table3[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table4[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table4[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table5[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table5[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table6[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table6[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table7[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table7[i][j]);
            }
        }
        else System.out.println("Data not found!");
    }
}

Try this: 尝试这个:

public void Data(String x){
    boolean b = false;
    for (int i = 1; i < 6; i++) {
        if (x.equalsIgnoreCase(table1[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table1[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table2[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table2[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table3[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table3[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table4[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table4[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table5[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table5[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table6[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table6[i][j]);
            }
        }
        else if (x.equalsIgnoreCase(table7[i][1])){
            for (int j = 0; j < 4; j++) {
                System.out.printf("|%-40s", table7[i][j]);
            }
        }
        else b = true;
    }
    if (b) System.out.println("Data not found!");
}

If you want to terminate as soon as data is not found, you can change your else statement to this: 如果要在找不到数据后立即终止,则可以将else语句更改为此:

else {
    System.out.println("Data not found!");
    return;
}

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

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