Results 1 to 2 of 2
  1. #1
    Alex89 is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default Variable issues!

    I am having problems fixing the varialbe issue with my code. The application I have created is used to average test scores. The problem is in the first time you ask how many tests there are to be averaged. They are stored in the result in testScore, but use that same variable to store the scores for your tests. Then in the "For" statement I am comparing the count to scoreCount. You see I do not know how to fix the variables to make the application run correctly. Any info would be greatly appreaciated. I am sorry if I did not put the code in something better. I do not know how to do that at the moment.


    Code:

    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class EnhancedTestScoreApp
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            String choice = "y";
            while (!choice.equalsIgnoreCase("n"))
            {
                // 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
            int scoreTotal = 0;
            int scoreCount = 0;
            int testScore = 0;
            int maximumScore = 0;
            int minimumScore = 100;
            // get a series of test scores from the user
           System.out.print("Enter the number of tests to be averaged:  ");
           				scoreCount = sc.nextInt();
           while (testScore != 999)
            {
                        // get the input from the user
                System.out.print("Enter score: ");
                    testScore = sc.nextInt();
                   for (int i = 0; i<= testScore; i++)
                   {
    				   scoreTotal = (scoreCount + testScore) *
    				  (1 + testScore);
    			   }
                   // accumulate score count and score total
                    if (testScore <= 100)
                    {
                            scoreCount += 1;
                            scoreTotal += testScore;
                            maximumScore = Math.max(maximumScore, testScore);
                            minimumScore = Math.min(minimumScore, testScore);
                            }
                            else if (testScore != 999)
                                    System.out.println("Invalid entry, not counted");
                    }
    
            // calculate the average score and display the results
            double averageScore = (double) scoreTotal / (double) scoreCount;
    
            NumberFormat number = NumberFormat.getNumberInstance();
            number.setMaximumFractionDigits(1);
    
            String message = "\n" +
                             "Score count:   " + scoreCount + "\n"
                           + "Score total:   " + scoreTotal + "\n"
                                   + "Average score: " + number.format(averageScore) + "\n"
                                   + "Minimum score: " + minimumScore + "\n"
                                   + "Maximum score: " + maximumScore + "\n";
    				// see if the user wants to continue
    				System.out.print("Would you like to enter more test scores? (y/n): ");
                    choice = sc.next();
                   System.out.println(message);
            }
    }
    }
    Last edited by JavaBean; 11-07-2007 at 08:05 PM. Reason: Code is placed inside [code] tag.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class ETSA
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);  // create a Scanner object
            String choice = "y";
            while (!choice.equalsIgnoreCase("n"))
            {
                // 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
                int scoreTotal = 0;
                int scoreCount = 0;
                int testScore = 0;
                int maximumScore = 0;
                int minimumScore = 100;
                // get a series of test scores from the user
                System.out.print("Enter the number of tests to be averaged:  ");
                // If you are going to use "scoreCount" like this then you will
                // need to decrement it for each successful entry and leave the
                // loop when it gets down to_or_below zero.
                // Or you could use a seperate variable for the counting.
    //            scoreCount = sc.nextInt();
                sc.nextInt();  // consumed but not used for now...
                while (testScore != 999)
                {
                    // get the input from the user
                    System.out.print("Enter score: ");
                    testScore = sc.nextInt();
    //                for (int i = 0; i<= testScore; i++)
    //                {
    //                    scoreTotal = (scoreCount + testScore) * (1 + testScore);
    //                }
                    // accumulate score count and score total
                    if (testScore <= 100)  // if(testScore >= 0 && testScore <= 100)
                    {
                        scoreCount += 1;
                        scoreTotal += testScore;
    //                    maximumScore = Math.max(maximumScore, testScore);
    //                    minimumScore = Math.min(minimumScore, testScore);
                        if(testScore > maximumScore)
                            maximumScore = testScore;
                        if(testScore < minimumScore)
                            minimumScore = testScore;
                    }
    //                else if (testScore != 999)
    //                    System.out.println("Invalid entry, not counted");
                }
    
                // calculate the average score and display the results
                double averageScore = (double) scoreTotal / (double) scoreCount;
    
                NumberFormat number = NumberFormat.getNumberInstance();
                number.setMaximumFractionDigits(1);
    
                String message = "\n" +
                            "Score count:   " + scoreCount + "\n"
                          + "Score total:   " + scoreTotal + "\n"
                          + "Average score: " + number.format(averageScore) + "\n"
                          + "Minimum score: " + minimumScore + "\n"
                          + "Maximum score: " + maximumScore + "\n";
                // see if the user wants to continue
                System.out.print("Would you like to enter more test scores? (y/n): ");
                choice = sc.next();
                System.out.println(message);
            }
        }
    }

Similar Threads

  1. iterator issues
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 02:43 PM
  2. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 02:16 AM
  3. Array issues
    By Neo82 in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 04:22 AM
  4. Compatibility issues with IE7
    By Aneesha in forum New To Java
    Replies: 7
    Last Post: 11-30-2007, 06:46 AM
  5. jcheckbox issues need help. thanks.
    By carlos123 in forum New To Java
    Replies: 3
    Last Post: 11-05-2007, 11:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •