Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem with calculating number entered to display prompts

$
0
0
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;

public class TestScoreApp
{
    // initialize variables and create a Scanner object
        private static double scoreTotal = 0;
        private static int scoreCount = 0;
        private static int testScore  = 0;
        private static double averageScore = 0;
        
        private static DecimalFormat df = new DecimalFormat("##0.0");
        private static NumberFormat number = NumberFormat.getNumberInstance();
        
    public static void main(String[] args)
    {
        SeeHeading.getHeading("Assignment 3");
        // display a welcome message
        System.out.println("  Test Score Application\n");
        // display operational messages
        
        // create the Scanner object
        Scanner sc = new Scanner(System.in);

        System.out.print("  Enter the number of test scores to be entered or a zero to exit ->      ");
        testScore = sc.nextInt();
        System.out.println();
        
        //User terminates the program without data entry
        if (testScore == 0) {
            System.out.println("\n  Test score program terminated by the User.");
            SeeDate.printfDate();
            SeeDate.printfTime();
            System.out.println();
            System.exit(0);   //research on your own Pages 484 and 485
        } //end if statement
       
        // get a series of test scores from the user
        while (testScore != 0)
        {
            // get the input from the user
            System.out.print("  Enter score: ");
            testScore = sc.nextInt();
            //for (scoreCount = 0; scoreCount < testScore; scoreCount++) {
                //score[scoreCount] = sc.nextInt();
                //scoreTotal += score[scoreCount];
            //}
            // accumulate score count and score total
            if (testScore <= 100)
            {
                scoreCount = scoreCount + 1;
                scoreTotal = scoreTotal + testScore;
                averageScore = scoreTotal / scoreCount;
                 
            } // end if statement
            else if (testScore >= 100) 
                System.out.println("  Invalid entry, not counted");
        
        number.setMaximumFractionDigits(0);
        String scoreTotalString = number.format(scoreTotal);
        
        // display the score count, score total, and average score        
        String message = "\n" +
                         "  SUMMARY" + "\n"
                       + "  Score count:   " + scoreCount + "\n"
                       + "  Score total:   " + scoreTotalString + "\n"
                       + "  Average score: " + df.format(averageScore) + "\n";
                       System.out.println(message);
                       
        System.out.print("  Enter the number of test scores to be entered or a zero to exit ->      ");
        testScore = sc.nextInt();
        System.out.println();
        
        //User terminates the program without data entry
        if (testScore == 0) {
            System.out.println("\n  Test score program terminated by the User.");
            SeeDate.printfDate();
            SeeDate.printfTime();
            System.out.println();             
         } //end if statement
      } // end while statement
   } // end main
} // end class


What I'm having issues with is when I prompt a user to enter a number of test scores, if they enter a number greater 1 the Enter Score prompt is suppose to display the number of times they requested.

So if they enter a 4 "Enter Score:" is to display a total of 4 times. What is happening at this point is no matter how many numbers I put in only 1 displays and the prompt to enter test scores appears again and goes through the loop.

Test Score Application

Enter the number of test scores to be entered or a zero to exit -> 5

Enter score: 45

SUMMARY
Score count: 1
Score total: 45
Average score: 45.0

Enter the number of test scores to be entered or a zero to exit -> 5

Enter score: 102
Invalid entry, not counted

SUMMARY
Score count: 1
Score total: 45
Average score: 45.0

Enter the number of test scores to be entered or a zero to exit -> 0


Test score program terminated by the User.


What is suppose to happen is when a 5 is entered they are prompted again 5 times to enter test scores but right now that's not happening. Also if they enter a number greater than 100 they are to be notified and then prompted again however the loop is just starting over again instead. Very confused. I'm sure it's something simple that I'm missing. I hope my question made sense.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>