Issues with loop skipping every other input in java

Course Queries Syllabus Queries 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm trying to create this rather simple program for my java class. Everything is com/tag/working">working, except for when I tried to have an input loop. I've never done that before, and it's ignoring every other input. Here is the com/tag/problem">problem com/tag/prompt">prompt:

B. Ch. 4 – Average - Write a program that will read an unspecified number of integer grades and find the summary total and average. Print grades, total and average. The last record will be the trailer record of -1. Also output the final letter grade per syllabus grading scale.

And here is the code:

import java.util.Scanner;
public class Average
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        com/tag/float">float counter = 0;
        com/tag/float">float accum = 0;
        com/tag/float">float addAccum = 0;
        com/tag/float">float tempLoop = 0;
        System.out.println("Please Enter Grade, Enter -1 to Finish: ");

        while (tempLoop != -1)
        {
            addAccum = in.nextFloat();
            counter++;
            accum = addAccum + accum;
            tempLoop = in.nextFloat();
        }
        com/tag/float">float avgGrade = accum / counter;
        if(avgGrade >= 90)
        {
            System.out.println("\nYour Grade is: " + "A");
        }else if(avgGrade >=80)
        {
            System.out.println("\nYour Grade is: " + "B");
        }else if(avgGrade >=70)
        {
            System.out.println("\nYour Grade is: " + "C");
        }else if(avgGrade >=60)
        {
            System.out.println("\nYour Grade is: " + "D");
        }else
        {
            System.out.println("\nYour Grade is: " + "F");
        }
        System.out.println("\nGrade Total: " + accum);
        System.out.println("\nCounter Num :" + counter); // for testing only
        System.out.println("\nAverage Grade: " + avgGrade);
    }
}

This is the console input/output:

Please Enter Grade, Enter -1 to 
                                                
                                                
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.