gpt4 book ai didi

java - 迭代二维数组的第一列后仅打印一次消息

转载 作者:行者123 更新时间:2023-12-01 18:40:57 25 4
gpt4 key购买 nike

导入java.util.Scanner;

公开课成绩{

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

//请求学生代码。

    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++) {

//发现= 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");

}

}
}
}

}

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

最佳答案

当您迭代搜索时,不需要第二个循环来迭代属性,因为您不使用它

 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

关于java - 迭代二维数组的第一列后仅打印一次消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59933163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com