我试图打印“学费会增加”,当至少有三名学生平均达到70%以上时,如果少于3名学生平均达到70%以上,则“学费不会增加”。我的问题是,即使平均值不高于70%,该计划也将打印出“学费会增加”。我不确定我做错了什么。我试过改变if语句,我觉得我错过了很简单的东西。
public static void main(String[] args)
{
// Create Scanner
Scanner input = new Scanner(System.in);
// Create array to store 5 students and 3 classes per student
int[][] grades = new int[5][3];
// Create array to store students first and last name
String[] names = new String[5];
// Create array to store average scores
double[] avgScores = new double[5];
// Prompt user to input name
for (int i = 0; i < names.length; i++)
{
System.out.print("Enter the student's first and last name: ");
names[i] = input.nextLine();
}
double average = 0;
int score; // student's grade
int finalGrade = 0; // collective grades
int goodStudents = 0; // students that received average of 70+
// Prompt user to input grades using nested for loop
for (int i = 0; i <= 4; i++) // from student 1 to student 5
{
for (int k = 0; k <= 2; k++) // from class 1 to class 3
{
System.out.print("Enter the grade for class " + (k + 1) + " " + "for student "
+ (i + 1) + " : ");
grades[i][k] = input.nextInt();
score = grades[i][k];
finalGrade = finalGrade + score;
}
// Calculate the average score for the 3 classes
avgScores[i] = finalGrade / 3;
finalGrade = 0; // reset
}
for (int j = 0; j <= 4; j++)
{
if (avgScores[j] < 70)
{
goodStudents++;
}
}
// count the number of students that have 70+ average
if (goodStudents >= 3)
{
System.out.println("Tuition will be increased by 10% next semester.");
} else if (goodStudents < 3)
{
System.out.println("Tuition will not be increased.");
}
}
答案 0 :(得分:2)
我很确定你的逻辑不正确
< 70
您的标准至少有三名学生平均为70%+ ,但您正在测试if (avgScores[j] >= 70)
{
goodStudents++;
}
。改变它,
/Users/useraccount/Google Drive Family/code/Rails/12-Week-Challenge/Week_0 [Dry Run]/testapp/
答案 1 :(得分:0)
您需要获得分数的最大值并获得百分比(70%),稍后您必须使用该分数。 这是其中一种方式:
import java.util.Scanner;
class Dot {
public static void main(String[] args) {
// Create Scanner
Scanner input = new Scanner(System.in);
int max_score = 5; // Max score
double persn = 5 *0.7; // 0.7 is 70%
// Create array to store 5 students and 3 classes per student
int[][] grades = new int[5][3];
// Create array to store students first and last name
String[] names = new String[5];
// Create array to store average scores
double[] avgScores = new double[5];
// Prompt user to input name
for (int i = 0; i < names.length; i++) {
System.out.print("Enter the student's first and last name: ");
names[i] = input.nextLine();
}
double average = 0;
int finalGrade = 0; // collective grades
int goodStudents = 0; // students that received average of 70+
// Prompt user to input grades using nested for loop
for (int i = 0; i <= 4; i++) // from student 1 to student 5
{
for (int k = 0; k <= 2; k++) // from class 1 to class 3
{
System.out.print("Enter the grade for class (max = )"+max_score+ + (k + 1) + " " + "for student "
+ (i + 1) + " : ");
grades[i][k] = input.nextInt();
finalGrade += grades[i][k];
}
// Calculate the average score for the 3 classes
avgScores[i] = finalGrade / 3.0;
finalGrade = 0; // reset
}
for (int j = 0; j < 5; j++) {
if (avgScores[j] <= persn) { //here is 70% as persn
goodStudents++;
}
}
// count the number of students that have 70+ average
if (goodStudents >= 3) {
System.out.println("Tuition will be increased by 10% next semester.");
} else if (goodStudents < 3) {
System.out.println("Tuition will not be increased.");
}
}
}