Results 1 to 3 of 3
Thread: Finding math.min/max and total
- 03-17-2012, 09:19 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Finding math.min/max and total
I am working on a project for my java class and got stumped on a few questions. I am requesting guidance on figuring out the math.min, math.max and total of scores. Should I be creating a method and calling it to find the max, min, and total or should they be included in getScoreWithinRange?
Java Code:// import necessary packages import java.util.Scanner; // class declaration public class ProjectApp { // main method declaration public static void main(String[] args) { // start of while loop String choice = "y"; while (choice.equalsIgnoreCase("y")) { // create scanner Scanner sc = new Scanner(System.in); int testNumber = getTestNumberWithinRange(sc, "How many scores would you like to enter? ", 6, 10); double score = getScoreWithinRange(sc, 1, 10, testNumber); } } public static int getTestNumber(Scanner sc, String prompt) { int testNumber = 0; boolean isValid = false; while (isValid == false) { System.out.print(prompt); if (sc.hasNextInt()) { testNumber = sc.nextInt(); isValid = true; } else { System.out.println("Error! Invalid number. Try Again."); } sc.nextLine(); } return testNumber; } public static int getTestNumberWithinRange(Scanner sc, String prompt, int min, int max) { int testNumber = 0; boolean isValid = false; while (isValid == false) { testNumber = getTestNumber(sc, prompt); if (testNumber < min) { System.out.println("Error! Number must be greater than " + min + "."); } else if (testNumber > max) { System.out.println("Error! Number must be less than " + max + "."); } else isValid = true; } return testNumber; } public static double getScore(Scanner sc, String prompt) { double score = 0; boolean isValid = false; while (isValid == false) { System.out.println(prompt); if (sc.hasNextDouble()) { score = sc.nextDouble(); isValid = true; } else { System.out.println("Error! Invalid number. Try Again."); } sc.nextLine(); } return score; } public static double getScoreWithinRange(Scanner sc, double min, double max, int testNumber) { double score = 0; boolean isValid = false; while (isValid == false) { for (int i = 1; i <= testNumber; i++) { System.out.print("Enter score #" + i + ": "); score = sc.nextDouble(); if (score <= max && score >= min) { isValid = true; } else if (score >= max) { System.out.println("Error! Number must be 10.00 or less"); i--; } else if (score <= min) { System.out.println("Error! Number must be 1.00 or more"); i--; } } } return score; } } // end class
- 03-17-2012, 10:40 PM #2
Re: Finding math.min/max and total
Generally you would write a method to do one thing only. So you could write separate method s to do each task. However, in this case you can determine the max and min as each value is read in. Read in a new value and compare it to the old max value. If the new value is higher make it the max value.
- 03-18-2012, 12:46 AM #3
Similar Threads
-
Running total little help
By silverspoon34 in forum Forum LobbyReplies: 9Last Post: 02-27-2011, 09:19 PM -
Calculating Total?
By ethemartian in forum New To JavaReplies: 6Last Post: 02-21-2011, 04:09 AM -
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12-07-2010, 06:23 AM -
Issue With Finding Total Number of Blank Spaces in File
By Cod in forum New To JavaReplies: 2Last Post: 12-10-2009, 12:06 PM -
Total noob
By J_Walker in forum New To JavaReplies: 9Last Post: 04-24-2009, 03:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks