Results 1 to 9 of 9
- 03-25-2011, 05:44 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Finding minimum and maximum of 'n' numbers...please help!
So I am new to programming and I am sorry if I am posting this is the wrong spot also. :o Hopefully someone will be able to help me figure out my problem! My problem is that I do not know how to find the minimum and maximum of 'n' numbers using and array or loop. The program is supposed to find the minimum, maximum, and average of 'n' number of test scores. I found out how to do the average but not the minimum of maximum. Here is my program so far:
import java.util.Scanner;
public class TestScores {
// the number of tests
final static int NUM_TESTS = 10;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
// input the number of tests
System.out.print("Enter the Number of Tests: ");
int numberOfTests = input.nextInt();
// initializing the loop
int testScore;
int total = 0;
int i = 1;
while (i <= numberOfTests){
// input test scores
System.out.print("Enter Next Test Score: ");
testScore = input.nextInt();
total = total + testScore;
++i;
}
// calculate the average score
double testAverage = total / numberOfTests;
// output the minimum, maximum, and average score
System.out.println("Test Minimum: " );
System.out.println("Test Maximum: " );
System.out.println("Test Average: " + testAverage);
}
}
- 03-25-2011, 08:36 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
Declare two more variables, one for maxTestScore and one for minTestScore. As you are adding up the test scores, keep track of the highest and lowest scores you've seen so far. You'll need a couple of if statements to test for the highest and lowest scores. You can initialize your maxTestScore to 0 and minTestScore to 100. When you're doing the comparisons, you'll see why.
- 03-25-2011, 08:59 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 03-25-2011, 09:06 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
-
you can do something similar for the smallest number tooJava Code:int largest; int[] intArray = {5,4,6,7,2,3,8,1,9,0}; for (int i=0; i<intArray.length; i++) { if (largest < intArray[i]) { largest = intArray[i]; } }
- 03-25-2011, 11:02 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
ozzyman, I tried using the code that you suggested and when I try to compile the program I get the following error:
TestScores.java:41: i is already defined in main(java.lang.String[])
for (int i = 0; i<intArray.length; i++) {
^
Do you know how to fix that error? If i just take that "int i = 0" out, the program doesn't compile. And thanks for all the help everyone! I think i'm starting to get it!
-
it shouldn't matter that 'i' was defined before because for (int i=0....) is naming and using a Local variable 'i' and it shouldnt be a problem that you have a different variable named 'i'.
anyhow, use a different variable name and tell me if there are any errors e.g.
for (int x=0; x<intArray.length; x++) {
- 03-25-2011, 11:27 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Well i changed i to z and it worked. It is very strange how like you said, it's a local variable and shouldn't have been a problem but oh well at least now it works. But I have another question. Since in my program I have a loop for the user to enter in how many tests they are entering then whatever amount they say, the program prompts them that many times to enter in a test Score. So the question I am getting to is in the array, how do I put the scores they entered in that string since the user could be putting in random numbers and not set numbers?
-
in future you need to put separate topics in separate posts to save confusion, get more people to answer your question, and help others who are finding these posts through search engines to answer their own questions.
try to make crystal-clear what you are asking too. i got the part that you want to ask the user as many times as needed to input test scores. but the rest is unclear to me:
how do I put the scores they entered in that string since the user could be putting in random numbers and not set numbers
are you looking for some sort of validation? i.e. value entered must be between two values, e.g. scores cannot be below 0 and max score is 100, therefore:
Java Code:int MIN_SCORE = 0; int MAX_SCORE = 100; if (scoreInput >= MIN_SCORE && scoreInput <= MAX_SCORE) { //code to input the score } else { System.out.println("Sorry - that is not a valid score. Would you like to try again?"); //code for user options }
Similar Threads
-
Finding maximum number at index position
By Shyamz1 in forum New To JavaReplies: 9Last Post: 10-27-2010, 08:14 PM -
Maximum Minimum Values
By z6StringAssasin in forum New To JavaReplies: 2Last Post: 05-09-2010, 12:36 AM -
Calculating Maximum and Minimum
By Ejava in forum New To JavaReplies: 6Last Post: 03-01-2010, 04:36 PM -
[SOLVED] Help Finding the Minimum value of an array
By MSteinman in forum New To JavaReplies: 18Last Post: 04-21-2009, 09:14 AM -
To find the Maximum and Minimum in an Array of Strings
By luscious in forum JavaServer Pages (JSP) and JSTLReplies: 9Last Post: 07-31-2008, 01:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks