Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Course Queries Syllabus Queries 2 years ago
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.
Turn Your Knowledge into Earnings.
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 REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
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.
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.
Course Queries 4 Answers
Course Queries 5 Answers
Course Queries 1 Answers
Course Queries 3 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.