8 Replies Latest reply on Oct 23, 2006 3:09 AM by 807607

# How to calculate Lowest number using While or if

Hi all,
The program basically has to have 6 numbers inputted. Theoretically these are test scores. It then has to calculate the lowest number.
Im stuck trying to figure out how to arrange the if statement. Anyone know how to do this.

You would need to store the number if it is lower than the first. THis is my code so far
`````` import java.util.Scanner;

public class TestScore
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner(System.in);

double numOne = keyboard.nextDouble();
double numTwo = keyboard.nextDouble();
double numThree = keyboard.nextDouble();
double numFour = keyboard.nextDouble();
double numFive = keyboard.nextDouble();
double numSix = keyboard.nextDouble();

System.out.print("Your scores are: " + numOne +" "+ numTwo +" "+ numThree + " ");
System.out.println(numFour +" "+ numFive +" " + numSix);

int n = numOne;
int count = 1;

while (count <= 6)
{

if()
{

}

count++;
}

}
}``````
• ###### 1. Re: How to calculate Lowest number using While or if
You have declared six variables to store your six numbers. What happens if the requirements change to store 100 numbers? Are you going to change your program to declare another 94 variables? A better solution wouold be to store the numbers in an array.

Declare a variable to store the lowest number. Is this what n is for? If so give it a better name. Then loop over your array and compare each number to "n" and if it is lower assign that value to "n".
• ###### 2. Re: How to calculate Lowest number using While or if
Try storing the scores in an array. Then you could loop through the array like so:
``````int lowest = Double.MAX_VALUE;
for(int i=0; i<scores.length; i++)
{
if(scores[i] < lowest)
{
lowest = scores;
}
}

Does that make sense? ``````
• ###### 3. Re: How to calculate Lowest number using While or if
Sorry cant do array i also forgot to mention its only supposed to work for 6 scores.

Cant do array because we havent learned it yet. Gotta use if and while.
Any ideas???
• ###### 4. Re: How to calculate Lowest number using While or if
In that case, you dont need to use while, you just need five if statements.
• ###### 5. Re: How to calculate Lowest number using While or if
i thought it would be easier to do something like an if inside a while
i didnt want to do 5 if statements i thought tehre was an easier way
• ###### 6. Re: How to calculate Lowest number using While or if
There is! Use an array but if you say you can't use one then you are stuck with a bunch of if statements.
• ###### 7. Re: How to calculate Lowest number using While or if
Use an array, maybe your teacher will give you extra credit!
• ###### 8. Re: How to calculate Lowest number using While or if
k i will thx for the help