// I created a simple java code which displays students grade according to the marks they got. There is no compilation error, but all grades are displayed as D. So please help me how can i make it to display correctly.
// The code is as follows.
class grade { public static void main (String[]arguments) { int totalmarks = 400; int marks = 0; String grade; if (marks < 100) grade = "D"; else if (marks < 200) grade = "C"; else if (marks < 300) grade = "B"; else if (marks <= 400) grade = "A"; // student1 got 400 marks. marks = 400; System.out.println("marks scored by student1 are " + marks); System.out.println(grade); // student2 got 352 marks. marks = 352; System.out.println("marks scored by student2 are " + marks); System.out.println(grade); //student3 got 300 marks. marks = 300; System.out.println("marks scored by student3 are " + marks); System.out.println(grade); //student4 got 253 marks. marks = 253; System.out.println("marks scored by student4 are " + marks); System.out.println(grade); //student5 got 200 marks. marks = 200; System.out.println("marks scored by student5 are " + marks); System.out.println(grade); //student6 got 150 marks. marks = 150; System.out.println("marks scored by student6 are" + marks); System.out.println(grade); //student7 got 100 marks. marks = 100; System.out.println("marks scored by student7 are " + marks); System.out.println(grade); //student8 got 50 marks. marks = 50; System.out.println("marks scored by student8 are " + marks); System.out.println(grade); //student9 got 25 marks. marks = 25; System.out.println("marks scored by student9 are " + marks); System.out.println(grade); //student10 got 0 marks. marks = 0; System.out.println("marks scored by student10 are " + marks); System.out.println(grade); } }
EDIT: All the part of the program thinks that the marks variable value is only equal to the value when it is initialised (= 0). Hence it shows D grade for all cases. How can i overcome this.