簡體   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