gpt4 book ai didi

java - 如果考试成绩为负,如何让用户在第二个 for 循环中重新输入考试成绩?

转载 作者:行者123 更新时间:2023-12-01 12:27:52 24 4
gpt4 key购买 nike

import java.util.Scanner;

public class GradeCalculator {

public static void main(String[] args) {

double examAdd = 0;
double examAvg = 0;
double exam = 0;
int j = 0;

Scanner s = new Scanner(System.in);

System.out.print("Please enter the number of students: ");
int stuNum = s.nextInt();

System.out.print("Please enter the number exams : ");
int examNum = s.nextInt();


for (int i = 1; i <= stuNum; i++) {
Scanner readName = new Scanner(System.in); /* had to re-make another scanner, had issues without one inside the for loop JUST with "nextLine()" nothing else*/

System.out.println("\n--------------------------------------");

System.out.print("Enter student " + i + "'s name : " );
String name = readName.nextLine();

System.out.print("Enter exam scores : ");

for (j = 0; j < examNum; j++) {
exam = s.nextDouble();
examAdd = (examAdd + exam);

}
examAvg = (examAdd/examNum);

System.out.println("Grade Statistics for " + name);
System.out.println(" Average : " + examAvg);

if (examAvg <= 100 & examAvg >= 90){
System.out.println (" Letter Grade: A \n" + " " + name + " gets 4 stars! ****\n");
examAvg = 0; /* Restart the average formula to calculate next students average*/
examAdd = 0;
}
else if (examAvg <= 89.99 & examAvg >= 80){
System.out.println(" Letter Grade: B \n" + " " + name + " gets 3 stars! ***\n");
examAvg = 0; /* Restart the average formula to calculate next students average*/
examAdd = 0;
}
else if (examAvg <= 79.99 & examAvg >= 70){
System.out.println(" Letter Grade: C \n" + " " + name + " gets 2 stars! **\n");
examAvg = 0; /* Restart the average formula to calculate next students average*/
examAdd = 0;
}
else if (examAvg <= 69.99 & examAvg >= 60){
System.out.println(" Letter Grade: D \n" + " " + name + " gets 1 star! *\n");
examAvg = 0; /* Restart the average formula to calculate next students average*/
examAdd = 0;
}
else if (examAvg <= 59.99) {
System.out.println(" Letter Grade: F \n" + " " + name + " gets 0 stars!\n");
examAvg = 0;/* Restart the average formula to calculate next students average*/
examAdd = 0;
}

}



}




}

在第二个 for 循环中,我似乎无法弄清楚如何让用户重新输入他/她的考试成绩输入(如果考试成绩为负数)。这就是我到目前为止所拥有的。

最佳答案

   for (j = 0; j < examNum; j++) {
exam=-1
while (exam<0){

exam = s.nextDouble();
examAdd = (examAdd + exam);
if (exam<0)
System.out.println("negative write again");

}

}

    for (j = 0; j < examNum; j++) {

while (true){

exam = s.nextDouble();
examAdd = (examAdd + exam);
if (exam<0)
System.out.println("negative write again");

else
continue;

}

关于java - 如果考试成绩为负,如何让用户在第二个 for 循环中重新输入考试成绩?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26188291/

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