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
    }
vkg
  • 1,839
  • 14
  • 15
user3670236
  • 133
  • 1
  • 3
  • 13

3 Answers3

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];
}
Leandri
  • 127
  • 1
  • 13
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
TGH
  • 38,769
  • 12
  • 102
  • 135
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

Community
  • 1
  • 1
ubergoober
  • 119
  • 5