平均不正确的小数点在我的成绩单程序中

huangapple go评论73阅读模式
英文:

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&#39;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&#39;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(&quot;What is your name? &quot;);
String course = readLine(&quot;What course would you like to calculate the average of? &quot;);
System.out.println(&quot;&quot;);
int gradenum = 1;
int count = 0;
double average = 0;
double sum = 0;
double newcount = 0;
while(true)
{
int grade = readInt(&quot;Enter grade #&quot; + gradenum++ + &quot; (enter -1 to exit): &quot;);
count++;
sum += grade;
newcount = count - 1;
average = sum/newcount;
if(grade == -1)
{
gradenum = 1;
}
if(grade == -1)
{
if(average &gt;= 90 &amp;&amp; average &lt;= 100)
{
System.out.print(name);
System.out.print(&quot;, your average in &quot;);
System.out.print(course);
System.out.print(&quot; is &quot;);
System.out.print(average);
System.out.println(&quot;%. That&#39;s an A!&quot;);
System.out.println(&quot;Keep up the hark work!&quot;);
System.out.println(&quot;&quot;);
}
if(average &lt; 90 &amp;&amp; average &gt;= 80)
{
System.out.print(name);
System.out.print(&quot;, your average in &quot;);
System.out.print(course);
System.out.print(&quot; is &quot;);
System.out.print(average);
System.out.println(&quot;%. That&#39;s a B!&quot;);
System.out.println(&quot;You&#39;re doing a fine job!&quot;);
System.out.println(&quot;&quot;);
}
if(average &lt; 80 &amp;&amp; average &gt;= 70)
{
System.out.print(name);
System.out.print(&quot;, your average in &quot;);
System.out.print(course);
System.out.print(&quot; is &quot;);
System.out.print(average);
System.out.println(&quot;%. That&#39;s a C!&quot;);
System.out.println(&quot;You&#39;re passing the class.&quot;);
System.out.println(&quot;&quot;);
}
if(average &lt; 70 &amp;&amp; average &gt;= 60)
{
System.out.print(name);
System.out.print(&quot;, your average in &quot;);
System.out.print(course);
System.out.print(&quot; is &quot;);
System.out.print(average);
System.out.println(&quot;%. That&#39;s a D.&quot;);
System.out.println(&quot;You&#39;re close to passing. You can do it!&quot;);
System.out.println(&quot;&quot;);
}
if(average &lt;= 60)
{
System.out.print(name);
System.out.print(&quot;, your average in &quot;);
System.out.print(course);
System.out.print(&quot; is &quot;);
System.out.print(average);
System.out.println(&quot;%. That&#39;s an F.&quot;);
System.out.println(&quot;I think you need to ask your teacher for help.&quot;);
System.out.println(&quot;&quot;);
}
}
while(grade == -1)
{
String y = &quot;y&quot;;
String n = &quot;n&quot;;
String repeat = readLine(&quot;Would you like to check another course? (y/n) &quot;);
if(repeat.equals(n))
{
System.out.println(&quot;&quot;);
System.out.println(&quot;Good bye!&quot;);
}
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(&quot;, your average in &quot;);
System.out.print(course);
System.out.print(&quot; is &quot;);
System.out.print(average);
if (average &gt;= 90)
{
System.out.println(&quot;%. That&#39;s an A!&quot;);
System.out.println(&quot;Keep up the hark work!&quot;);
}
else if (average &gt;= 80)
{
System.out.println(&quot;%. That&#39;s a B!&quot;);
System.out.println(&quot;You&#39;re doing a fine job!&quot;);
}
else if (average &gt;= 70)
{
System.out.println(&quot;%. That&#39;s a C!&quot;);
System.out.println(&quot;You&#39;re passing the class.&quot;);
}
else if (average &gt;= 60)
{
System.out.println(&quot;%. That&#39;s a D.&quot;);
System.out.println(&quot;You&#39;re close to passing. You can do it!&quot;);
}
else
{
System.out.println(&quot;%. That&#39;s an F.&quot;);
System.out.println(&quot;I think you need to ask your teacher for help.&quot;);
}
System.out.println(&quot;&quot;);

huangapple
  • 本文由 发表于 2020年10月10日 12:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64289988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定