Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2007, 06:54 PM
Member
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0
Alex89 is on a distinguished road
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:

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 07:05 PM. Reason: Code is placed inside [code] tag.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-07-2007, 07:51 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
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);
        }
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
iterator issues orchid New To Java 2 08-12-2008 01:43 PM
Issues with Jva I.O Annatar01 New To Java 0 02-08-2008 01:16 AM
Array issues Neo82 New To Java 1 12-31-2007 03:22 AM
Compatibility issues with IE7 Aneesha New To Java 7 11-30-2007 05:46 AM
jcheckbox issues need help. thanks. carlos123 New To Java 3 11-05-2007 10:37 PM


All times are GMT +2. The time now is 03:36 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org