Hello, and thanks for providing this forum to people like me for help with programming.
I have searched the forums and actually found two or three threads that involve the same assignment that I am currently trying to complete, however my code is not very similar and I could not find the answer to my specific question.
The assignment is for beginners (I am definitely a beginner) and requires modifying provided code so that it functions properly/differently.
The code is for an application that prompts and accepts input from the user, the values of which are test scores, calculates the average of the scores, and then prints the results to the user. Values up to 100 are valid, and anything over results in an error message stating the input as invalid. Entering 999 is supposed to end the input and print the results, and not give the invalid error message.
I have modified the code so that it accepts input values up to 100 and displays the error message for values over 100, as well as completing input and calculating when 999 is entered, however my problem is that I can't seem to get it to not display the error message for 999 as well.
As I said everything works, but it also prints the error message for 999 before correctly calculating and finishing.
Here is my code:
import java.util.Scanner; public class ModifiedTestScoreApp { public static void main(String[] args) { // display operational messages System.out.println("Please enter test scores that range from 0 to 100."); System.out.println("To end the program enter 999."); System.out.println(); // print a blank line // initialize variables and create a Scanner object double scoreTotal = 0; int scoreCount = 0; int testScore = 0; Scanner sc = new Scanner(System.in); // get a series of test scores from the user while (testScore != 999) { // get the input from the user System.out.print("Enter score: "); testScore = sc.nextInt(); // accumulate score count and score total if (testScore <= 100) { scoreCount = scoreCount + 1; scoreTotal = scoreTotal + testScore; } if (testScore > 100 && testScore != 999) System.out.println("Invalid entry, not counted"); } // display the score count, score total, and average score double averageScore = scoreTotal / scoreCount; String message = "\n" + "Score count: " + scoreCount + "\n" + "Score total: " + scoreTotal + "\n" + "Average score: " + averageScore + "\n"; System.out.println(message); } }
As you can see, I tried to use the != operator to exclude 999 from displaying the error message, but it is not working.
I also tried something like testScore > 100 && testScore < 999, but it didn't seem to work either.
I suspect the problem may be that those statements are within the while (testScore != 999) loop, but I'm not sure.
Also, as this is a beginner's assignment, I believe I need to keep the code simple and use only statements similar to those that are currently being used.
I hope this post is clear, and thank you for any help!