Issues with loop skipping every other input in java

Course Queries Syllabus Queries 2 years ago

0 2 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 (2)

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 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 problem 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 de">code:

import java.util.Scanner;
public class Average
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        float counter = 0;
        float accum = 0;
        float addAccum = 0;
        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();
        }
        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
profilepic.png
manpreet 2 years ago

You have in.nextFloat() twice in your while loop. Change your logic to look for -1 first and then process the input.

Something like :

  tempLoop = in.nextFloat();
  while(tempLoop != -1){

     sum += tempLoop;
     tempLoop = in.nextFloat();
  }

Hope this helps.


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.