lundi 27 janvier 2020

Printing message only once after iterating through the first column of a 2d array

import java.util.Scanner;

public class Grade{

public static void main(String[] args) {

    String students[][] = new String[2][4];

    Scanner input = new Scanner(System.in);

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

        System.out.print("\n\nStudent 00" + (i + 1) + "\n\n");

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

            if (j == 0) {
                System.out.print("\n\tStudent Code : ");
            } else if (j == 1) {
                System.out.print("\n\tName : ");
            } else if (j == 2) {
                System.out.print("\n\tMaths Grade : ");
            } else if (j == 3) {
                System.out.print("\n\tFrench Grade : ");
            } else {
                System.out.print("\n\tNonexistent field!\n");
            }

            students[i][j] = input.nextLine();
        }

    }

    System.out.print("\n\tRegistered Students : \n\n");

    System.out.print("\tCODE\tFULL NAME\tMATHS\tFRENCH\n\n");

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

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

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

        }

        System.out.println();
    }

//Ask for student code.

    System.out.print("\n\tStudent Code : ");

    String search= input.nextLine();

    boolean found = false;

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

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

// found= true;

            if (search.equals(students[i][0])) {

                found = true;

                System.out.print("\n\tStudent Code Found!\n");

                String math = students[i][2];
                String french = students[i][3];

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

                Double f = new Double(french);
                double frenchConv = f.doubleValue();

                double average = (mathConv + frenchConv) / 2;

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

                if (average <= 40) {

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

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

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

                } else {

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

            }

            else if (!search.equals(students[i][0])) {

                found = false;

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

            }

        }
    }
}

}

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.

Aucun commentaire:

Enregistrer un commentaire