View Single Post
  #1 (permalink)  
Old 11-07-2007, 08:54 PM
Alex89 Alex89 is offline
Member
 
Join Date: Nov 2007
Posts: 4
Alex89 is on a distinguished road
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 09:05 PM. Reason: Code is placed inside [code] tag.
Reply With Quote
Sponsored Links