1

So I am making a program where you write 10 marks and it tells you the maximum and the minimum.

I Have a problem with the Minimum,as the maximum does work.

My problem is that instead of the worst mark,it shows me the first mark entered.

This Is Using Jcreator(Java)

   public void worstEnglish() {
        System.out.println("The Worst English Mark Is");
        //Array
        int worstEnglish = english[0];
        //Method
        for (int i = 0; i < english.length; i++) {
            if (english[i] < worstEnglish) {
                english[i] = worstEnglish;
            }//End Of If
        }//End of loop
    }
1
  • Add mode code to make it clear. Commented May 23, 2014 at 20:24

3 Answers 3

4

you are assigning the value of worstEnglish variable to english array instead of the other way around

if(english[i] < worstEnglish){
    worstEnglish = english[i];
}
Sign up to request clarification or add additional context in comments.

Comments

2
 worstEnglish = english[i];

Swap the assignment. Right now you are not updating the worstEnglish variable.

public void worstEnglish() {
System.out.println("The Worst English Mark Is");
//Array
int worstEnglish = english[0];
//Method
for (int i=0; i<english.length; i++){
if(english[i] < worstEnglish){
  worstEnglish = english[i];
}//End Of If
}//End of loop

Comments

0

Have you tried simplifying this with Collections.min() and Collections.max() instead of doing iterations over the Arrays?

Since you've typed the worstEnglish array as Int, I'm assuming these are Integer values in the array.

I'd image it'd be just worstEnglish = Collection.min(english);

See this SO for example: Finding the max/min value in an array of primitives using Java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.