英文:
Average incorrect by decimal points in my Report Card program
问题
public class MyProgram extends ConsoleProgram
{
public void run()
{
// Start here!
String name = readLine("What is your name? ");
String course = readLine("What course would you like to calculate the average of? ");
System.out.println("");
int gradenum = 1;
int count = 0;
double average = 0;
double sum = 0;
double newcount = 0;
while(true)
{
int grade = readInt("Enter grade #" + gradenum++ + " (enter -1 to exit): ");
if (grade == -1)
{
if (count > 0)
{
average = sum / count;
}
if (average >= 90 && average <= 100)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.printf("%.0f", average);
System.out.println("%. That's an A!");
System.out.println("Keep up the hard work!");
System.out.println("");
}
else if (average < 90 && average >= 80)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.printf("%.0f", average);
System.out.println("%. That's a B!");
System.out.println("You're doing a fine job!");
System.out.println("");
}
else if (average < 80 && average >= 70)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.printf("%.0f", average);
System.out.println("%. That's a C!");
System.out.println("You're passing the class.");
System.out.println("");
}
else if (average < 70 && average >= 60)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.printf("%.0f", average);
System.out.println("%. That's a D.");
System.out.println("You're close to passing. You can do it!");
System.out.println("");
}
else if (average <= 60)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.printf("%.0f", average);
System.out.println("%. That's an F.");
System.out.println("I think you need to ask your teacher for help.");
System.out.println("");
}
String repeat = readLine("Would you like to check another course? (y/n) ");
if (repeat.equals("n"))
{
System.out.println("");
System.out.println("Good bye!");
return;
}
else
{
count = 0;
sum = 0;
}
gradenum = 1;
}
else
{
count++;
sum += grade;
}
}
}
}
英文:
When I am calculating my average for my Report Card program, it is off by a few decimal numbers. When I add 98 + 100 I'm supposed to get 99 as the average. Instead, I am getting 98.5. I'm not sure if it is a problem with my count variable. Why is this and how can I fix it? My code is below.
Here is the output I am getting
What is your name? AG
What course would you like to calculate the average of? Algebra 2
Enter grade #1 (enter -1 to exit): 98
Enter grade #2 (enter -1 to exit): 100
Enter grade #3 (enter -1 to exit): -1
AG, your average in Algebra 2 is 98.5%. That's an A!
Keep up the hark work!
Would you like to check another course? (y/n) n
Good bye!
And here is the output I want
What is your name? AG
What course would you like to calculate the average of? Algebra 2
Enter grade #1 (enter -1 to exit): 98
Enter grade #2 (enter -1 to exit): 100
Enter grade #3 (enter -1 to exit): -1
AG, your average in Algebra 2 is 99%. That's an A!
Keep up the hark work!
Would you like to check another course? (y/n) n
Good bye!
public class MyProgram extends ConsoleProgram
{
public void run()
{
// Start here!
String name = readLine("What is your name? ");
String course = readLine("What course would you like to calculate the average of? ");
System.out.println("");
int gradenum = 1;
int count = 0;
double average = 0;
double sum = 0;
double newcount = 0;
while(true)
{
int grade = readInt("Enter grade #" + gradenum++ + " (enter -1 to exit): ");
count++;
sum += grade;
newcount = count - 1;
average = sum/newcount;
if(grade == -1)
{
gradenum = 1;
}
if(grade == -1)
{
if(average >= 90 && average <= 100)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.print(average);
System.out.println("%. That's an A!");
System.out.println("Keep up the hark work!");
System.out.println("");
}
if(average < 90 && average >= 80)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.print(average);
System.out.println("%. That's a B!");
System.out.println("You're doing a fine job!");
System.out.println("");
}
if(average < 80 && average >= 70)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.print(average);
System.out.println("%. That's a C!");
System.out.println("You're passing the class.");
System.out.println("");
}
if(average < 70 && average >= 60)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.print(average);
System.out.println("%. That's a D.");
System.out.println("You're close to passing. You can do it!");
System.out.println("");
}
if(average <= 60)
{
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.print(average);
System.out.println("%. That's an F.");
System.out.println("I think you need to ask your teacher for help.");
System.out.println("");
}
}
while(grade == -1)
{
String y = "y";
String n = "n";
String repeat = readLine("Would you like to check another course? (y/n) ");
if(repeat.equals(n))
{
System.out.println("");
System.out.println("Good bye!");
}
if(repeat.equals(n))
{
return;
//break;
}
}
}
}
}
答案1
得分: 1
不要在确认数字不是 -1 之前更新计数、总和和平均值:
if (grade == -1) {
gradenum = 1;
} else {
count++;
sum += grade;
newcount = count - 1;
average = sum / newcount;
}
此外,你在 if
块中有很多重复的代码。通过在顶部输出共同部分,你可以大大减少代码量。条件语句也可以简化:
System.out.print(name);
System.out.print(",你在");
System.out.print(course);
System.out.print("的平均分是");
System.out.print(average);
if (average >= 90) {
System.out.println("。这是一个 A!");
System.out.println("继续努力!");
} else if (average >= 80) {
System.out.println("。这是一个 B!");
System.out.println("你做得很好!");
} else if (average >= 70) {
System.out.println("。这是一个 C!");
System.out.println("你已经及格了。");
} else if (average >= 60) {
System.out.println("。这是一个 D。");
System.out.println("你离及格很近。你可以做到!");
} else {
System.out.println("。这是一个 F。");
System.out.println("我觉你需要向老师寻求帮助。");
}
System.out.println("");
英文:
Don't update your count, sum, and average until AFTER you've determined that the number was NOT a -1:
if(grade == -1)
{
gradenum = 1;
}
else
{
count++;
sum += grade;
newcount = count - 1;
average = sum/newcount;
}
Also, you have a lot of repeated code in your if
blocks. You can reduce your code by quite a bit by outputting the common parts at the top. The conditional statements can also be reduced:
System.out.print(name);
System.out.print(", your average in ");
System.out.print(course);
System.out.print(" is ");
System.out.print(average);
if (average >= 90)
{
System.out.println("%. That's an A!");
System.out.println("Keep up the hark work!");
}
else if (average >= 80)
{
System.out.println("%. That's a B!");
System.out.println("You're doing a fine job!");
}
else if (average >= 70)
{
System.out.println("%. That's a C!");
System.out.println("You're passing the class.");
}
else if (average >= 60)
{
System.out.println("%. That's a D.");
System.out.println("You're close to passing. You can do it!");
}
else
{
System.out.println("%. That's an F.");
System.out.println("I think you need to ask your teacher for help.");
}
System.out.println("");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论