Results 1 to 2 of 2
Thread: Variable issues!
- 11-07-2007, 06:54 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 4
- Rep Power
- 0
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 07:05 PM. Reason: Code is placed inside [code] tag.
- 11-07-2007, 07:51 PM #2
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
-
iterator issues
By orchid in forum New To JavaReplies: 2Last Post: 08-12-2008, 01:43 PM -
Issues with Jva I.O
By Annatar01 in forum New To JavaReplies: 0Last Post: 02-08-2008, 01:16 AM -
Array issues
By Neo82 in forum New To JavaReplies: 1Last Post: 12-31-2007, 03:22 AM -
Compatibility issues with IE7
By Aneesha in forum New To JavaReplies: 7Last Post: 11-30-2007, 05:46 AM -
jcheckbox issues need help. thanks.
By carlos123 in forum New To JavaReplies: 3Last Post: 11-05-2007, 10:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks