通过或失败的计数器是向后的。它使用哨兵并计数失败并通过。只有它倒数。如果您输入及格分数,则将其添加到失败等等。这是总代码:
我刚编辑了它......
import java.util.Scanner;
public class PassOrFail1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int total;
int gradeCounter;
int grade;
double average;
int passes = 0;
int fails = 0;
int studentCounter = 0;
total = 0;
gradeCounter = 0;
System.out.println("Enter a grade");
grade = input.nextInt();
while (grade != 000) {
total = total + grade;
gradeCounter++;
studentCounter++;
System.out.println("Enter a grade");
grade = input.nextInt();
if( grade >= 70 ){
passes++;
}
else
{
fails++;
}
}
if(gradeCounter != 0){
average = (double)total/gradeCounter;
System.out.printf("The average for the grades is %.2f\nGradeCounter is %11d",average,gradeCounter);
}
System.out.printf("\nNumber of Passes is %7d \nNumber of Fails is %8d\nand number of students is %d", passes, fails, studentCounter);
}
}
答案 0 :(得分:1)
您可以尝试使用此代码>>> !
public class PassOrFail {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int total;
int gradeCounter;
int grade;
double average;
int passes = 0;
int fails = 0;
int studentCounter = 0;
total = 0;
gradeCounter = 0;
System.out.println("Enter a grade");
grade = input.nextInt();
while (grade != -1) {
total = total + grade;
gradeCounter++;
studentCounter++;
if( grade >=70 ){
passes++;
}
else
{
fails++;
}
System.out.println("Enter a grade");
grade = input.nextInt();
}
if(gradeCounter != 0){
average = (double)total/gradeCounter;
System.out.printf("The average for the grades is %.2f\n GradeCounter is %d",average,gradeCounter);
}
System.out.printf("\nNumber of Passes is %d \n Number of Fails is %d ", passes, fails);
}
}