Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2007, 08:54 PM
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-07-2007, 09:51 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 03:43 PM
Issues with Jva I.O Annatar01 New To Java 0 02-08-2008 03:16 AM
Array issues Neo82 New To Java 1 12-31-2007 05:22 AM
Compatibility issues with IE7 Aneesha New To Java 7 11-30-2007 07:46 AM
jcheckbox issues need help. thanks. carlos123 New To Java 3 11-06-2007 12:37 AM


All times are GMT +3. The time now is 09:19 PM.


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