hi all
Hi

i build program. it is a selection. if else. where it ask the user to enter 3 marks for a student. the student pass if their grades is greater than or equal to 50 and they fail if their grades is less than 50, right

my problems is, it works but there is a logical error somewhere in the program. if i enter marks that is more than 50 it tell me they student fail when it should say pay and it tell me the same when i enter marks that is less than 50. tell me where i have gone awary. here is the codes that i use

double mark1, mark2,mark3, avemark, totalmarks = 0;

      double passmark=0;

       System.out.println("please enter mark 1");
       mark1 = userinput.nextDouble();

       System.out.println("please enter mark 2");
       mark2 = userinput.nextDouble();

       System.out.println("please enter mark 3");
       mark3 = userinput.nextDouble();

       avemark = (mark1 + mark2 + mark3);
       totalmarks = (totalmarks / 3);


       if(passmark >=50)
       {
           System.out.println("student pass");
       }
       else if(passmark <=50)
       {
           System.out.println("student fail");

correction
when i entered marks that is greater than or equal to 50 it should read the student pass and when marks enter less than 50 it should say student fail
but it stay fail when i enter marks that is greater than or equal to 50 and it say the same when mark enter less than 50.

First of all there is a problem with your variable naming. You use avemark to contain your total and totalmarks to contain your average. You should have

totalmarks = mark1 + mark2 + mark3;
avemark = totalmarks / 3.0;

You are comparing passmark to 50 but you only ever assign passmark the value of 0. You should be comparing avemark to 50.0 instead of passmark.

Edited 9 Months Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.