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

4

u/[deleted] Feb 15 '21

What exactly is the problem you're having?

0

u/Icy_Ad871 Feb 15 '21

Press J to jump to the feed. Press question mark to learn the rest of the keyboard shortcuts

Jump to content

Its supposed to receive the grades of three diferent classes and give the lowest average grade, but it doesnt do that.

5

u/[deleted] Feb 15 '21

Okay, and what is it actually doing?

0

u/Icy_Ad871 Feb 15 '21

It recieves the numbers of the first class, doesnt loop again and then it doesnt print anything

1

u/[deleted] Feb 15 '21

then it doesnt print anything

Well, you're not telling it to print anything else. Your only System.out.println() call is at the beginning of your program.

1

u/Icy_Ad871 Feb 15 '21

I did now i only get 0.0

1

u/[deleted] Feb 15 '21

Can you edit your post to show what you're doing? It's not clear what you're printing or where, and it's impossible for us to just guess.

1

u/Icy_Ad871 Feb 15 '21

Edited

1

u/[deleted] Feb 15 '21

You're initializing minave to 0, and then only setting it if ave is less than that, so it will never change.

-1

u/Icy_Ad871 Feb 15 '21

So what do i change?

→ More replies (0)

1

u/nagarjuna_reddy_27 Feb 15 '21

And why there are 2 If conditions doing the same thing?

1

u/Icy_Ad871 Feb 15 '21

i dont get what you mean

→ More replies (0)

1

u/Icy_Ad871 Feb 15 '21

Deleted the second if

4

u/NautiHooker Feb 15 '21

Well, if you explain what kind of problems then you might receive help.

Describe what you want to achieve and what does not work. If you have error messages then show them.

Please edit your post.

1

u/Icy_Ad871 Feb 15 '21

Its supposed to receive the grades of three diferent classes and give the lowest average grade, but it doesnt do that.

1

u/nagarjuna_reddy_27 Feb 15 '21

Could you edit the post and keep the original question for that program?

1

u/nagarjuna_reddy_27 Feb 15 '21

Can you post the original question?

0

u/GSD_101 Feb 15 '21
while (grade>=0){
			totalgrade=totalgrade+grade;
			grade = input.nextDouble();
			studentnum++;
		}

so you are trying to take input in repetition with it?

if yes , put this line in loop and not at start..

System.out.println("Enter grade of student number 1");
	double grade = input.nextDouble();

Since you know its 3 classes , no need to set counter.

do away with while statement.

but before you try this

what do do you mean by minimum average of 3 classes.

Cuz you should use loop to get inputs and sum them and outside the loop get average.