r/learnjava • • Feb 15 '21

I am having problems with this for

package For;
import java.util.Scanner;
public class lulatpreg3
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
	Scanner input=new Scanner(System.in);
	System.out.println("Enter grade of student number 1");
	double grade = input.nextDouble();
	double minave,ave;
	minave = 0;
	for (int i=1;i<3;i++){
		int studentnum = 1;
		double totalgrade = 0;
		while (grade>=0){
			totalgrade=totalgrade+grade;
			grade = input.nextDouble();
			studentnum++;
		}
		ave = totalgrade/studentnum;
		if (ave>minave){
			minave = ave;
	}

			
	}
	System.out.println(minave);
	}

}
0 Upvotes

22 comments sorted by

View all comments

Show parent comments

-1

u/Icy_Ad871 Feb 15 '21

So what do i change?

1

u/[deleted] Feb 15 '21

You need to initialize minave to something higher than zero if you want the code in your if statement to run, because right now, ave will never be lower than its initial value of zero.

1

u/Icy_Ad871 Feb 15 '21

I notices something i put < instead of > i edited

2

u/[deleted] Feb 15 '21

That's not going to fix the issue you're having (in fact, it'll now make minave be the maximum average). Take another look at your initial value for minave, that will help you.