简体   繁体   中英

Print a message only once after iterating 2d array

import java.util.Scanner;

public class Note {

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!");
            }

        }
    }
}

}

I need to display only one message after entering the code etudiant but istead it displays the message 4 times. The loop should only iterate through the first column of each line and compares it to what the user entered.

The indexing variable ( j ) of the last inner for-loop ( for (int j = 0; j < 4; j++) { ) is never used inside the loop, so you actually do not need that loop at all. General styling issues of your sample aside, you should rewrite the last loop like this:

    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]);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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