繁体   English   中英

迭代二维数组后只打印一次消息

[英]Print a message only once after iterating 2d array

导入 java.util.Scanner;

公共课注意{

public static void main(String[] args) {

    String etudiants[][] = new String[1][4];
    Scanner saisie = new Scanner(System.in);

    for (int i = 0; i < 1; i++) {

        System.out.print("\n\nEtudiant BTI00" + (i + 1) + "\n\n");

        for (int j = 0; j < 4; j++) {

            if (j == 0) {
                System.out.print("\n\tCode de l'etudiant : ");
            } else if (j == 1) {
                System.out.print("\n\tNom etudiant : ");
            } else if (j == 2) {
                System.out.print("\n\tNote Maths : ");
            } else if (j == 3) {
                System.out.print("\n\tNote Francais : ");
            } else {
                System.out.print("\n\tChamps inexistant!");
            }

            etudiants[i][j] = saisie.nextLine();
        }

    }

    System.out.print("\n\tEtudiants Enregistres : \n\n");

// System.out.print("\\tCode\\tNom\\t\\tMaths\\tFrancais\\n\\n");

    for (int i = 0; i < 1; i++) {

        for (int j = 0; j < 4; j++) {

            System.out.print("\t" + etudiants[i][j] + " ");

        }
    }

    System.out.println();

    System.out.print("\n\tEntrez code etudiant : ");

    String recherche = saisie.nextLine();

    boolean trouve = false;

    for (int i = 0; i < 1; i++) {

        for (int j = 0; j < 4; j++) {

            if (recherche.equals(etudiants[i][0])) {

                trouve = true;

                System.out.print("\n\tCode etudiant correct!");

                String math = etudiants[i][2];
                String francais = etudiants[i][3];

                Double m = new Double(math);
                double mathConv = m.doubleValue();

                Double f = new Double(francais);
                double francaisConv = f.doubleValue();

                double moyenne = (mathConv + francaisConv) / 2;

                System.out.print("\n\tMoyenne de l'etudiant : " + moyenne);

                System.out.print("\n\tEtudiant : " + etudiants[i][j]);

                if (moyenne <= 40) {

                    System.out.print("\n\tEchec!");

                } else if (moyenne > 40 && moyenne < 70) {

                    System.out.print("\n\tReprise!");

                } else {

                    System.out.print("\n\tSucces!");
                }

            } if (!trouve) {

                System.out.print("\n\tCode etudiant incorrect!");
            }

        }
    }
}

}

输入代码 etudiant 后,我​​只需要显示一条消息,但它会显示该消息 4 次。 循环应该只遍历每一行的第一列,并将其与用户输入的内容进行比较。

最后一个内部 for 循环( for (int j = 0; j < 4; j++) { )的索引变量 ( j ) 从未在循环内使用,因此您实际上根本不需要该循环。 除了示例的一般样式问题之外,您应该像这样重写最后一个循环:

    for (int i = 0; i < 1; i++) {

        // throw away this
        //for (int j = 0; j < 4; j++) {

        if (recherche.equals(etudiants[i][0])) {

            trouve = true;

            // ... rest of the code like you currently have it

            // You probably do not need this line too,
            // because you have almost the same in your second loop
            //System.out.print("\n\tEtudiant : " + etudiants[i][j]);
        }
    }

暂无
暂无

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

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