简体   繁体   中英

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.

Change this:

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

to this:

for (int j = 0; j < 1; j++)

as we want to run that loop only once.

Alternatively, you can remove the j loop altogether.

When you iterate for the search you don't need the second loop to iterate over the attributes, because you don't use it

 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++) { //you don't need this loop

it's recommended you use the length argument for those arrays, that way if you ever change the size, the loop won't break, in this case you can just get the first element of the array, and then iterate through that. So intead of

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

    }

you can say

        System.out.print("\n\nStudent 001" + "\n\n");

        for (int j = 0; j < students[0].length; 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[0][j] = input.nextLine();
        }

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