• 0

[Java] Score calculation program


Question

Hi. I wrote a program in Java to calculate users' HKCEE total score. (HKCEE is similar to GCSE, A grade represents 5 points; B grade represents 4 points...etc, F grade and U grade have 0 points.)

Here is the source code...

/*
 *This program is designed to calculate user's HKCEE total marks.
 *Written by Gundamdriver
 */
import javax.swing.JOptionPane;
public class CEMarks{
	public static void main(String args[])
	{
		//Define data type
		String  numberOfsubjects,
				grade;
		int	 numSub,
				total,
				counter;
		//Set the numSub, total and counter to zero
		numSub = 0;
		total = 0;
		counter = 1;

		//Ask for number of subjects
		numberOfsubjects = JOptionPane.showInputDialog("Enter the number of subjects:");
		//Convert value of numberOfsubjects to integer
		numSub = Integer.parseInt(numberOfsubjects);

		//Begin the while loop
		while (counter <= numSub){
			//Ask for the grades of the subjects
			grade = JOptionPane.showInputDialog("Enter the grade of the subject");
				//Use if to determine the corresponding mark to the grade
				if (grade == "A")
					total = total + 5;
				if (grade == "B")
					total = total + 4;
				if (grade == "C")
					total = total + 3;
				if (grade == "D")
					total = total + 2;
				if (grade == "E")
					total = total + 1;
				if (grade == "F")
					total = total + 0;
				if (grade == "U")
					total = total + 0;
				else
					JOptionPane.showMessageDialog(null, "Please input A, B, C, D, E, F or U", "Error", JOptionPane.INFORMATION_MESSAGE);
			//Add counter by 1
			counter = counter + 1;
		} 
		//Show the result
		JOptionPane.showMessageDialog(null, "Your total marks in HKCEE is " + total + ".", "Total marks", JOptionPane.INFORMATION_MESSAGE);
		//Terminate the program
		System.exit(0);
	}
}

Or please download the source code:

http://www.yousendit.com/transfer.php?acti...886E8186D0464BD

Here are my questions:

1. When I type in anything, the program just show the error "Please input A, B, C, D, E, F or U" (which I put in else). Why would this happen? Where is the bug?

2. When wrong "grade" is typed in, the counter will be added by 1, so I decided to add a statement in else:

counter = counter -1

But this leads to an infinity loop! Why would this happen? Because counter was substracted by 1 everytime the if / else statements run, and never reach the value of "numSub"?

3. Someone told me that in Java, variables are pointers, so I can't simply use grade == "A". But what does that mean?

Thanks for reading. :)

Link to comment
https://www.neowin.net/forum/topic/474977-java-score-calculation-program/
Share on other sites

3 answers to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.