![]() |
Simple java problem
Here is a simple program I made in Java that accepts an input for student type then calculates and outputs the student's grades. In my eyes the code should be working, though I'm a beginner in Java the thing looks to me to make perfect logical sense. Being such, it's kind of seriously pissing me off that it wont work. This code is amazingly ugly to me, but I'm still learning java from a book so I'm not sure how to compact this beast. Any feedback on this code will be helpful.
:
class Grades2 { |
A few things I noticed:
1) You have no import statements 2) You are using a bitwise 'and' in your selection statements. Change '&' to '&&'. 3) Is your code actually spaced as shown above? If so, there will be several syntax errors. |
I'm impressed that you were able to code for so long without testing your program. I only had to make a handful of minor changes to get it working.
As jaeusm mentioned, you need an import statement. :
import javax.swing.JOptionPane;:
if (response = null) {Double-quoted Strings cannot span multiple lines. So :
"Enter student type Due to a typo, you never declare the finalExamGrade variable before you attempt to set it. Correct as follows: :
int finalExamGrade = 0;:
finalNumericGrade = midterm * ENGLISH_MIDTERM_PERCENTAGE +:
finalNumericGrade = JOptionsPane.showMessageDialog(null, "*** SCIENCE STUDENT *** \n\n " +First off, the class you are attempting to use is JOptionPane, not JOptionsPane. Second, since you are trying to receive input, you should be calling JOptionPane.showInputPane; you no longer need to pass in the first parameter (null). In addition, since this method returns a String, you need to use the Integer.parseInt method to convert the input to the proper data type. :
finalNumericGrade = Integer.parseInt(JOptionPane.showInputDialog(As jaesum also mentioned, & does not do what you think it does. Use &&. One last thing... you seem confused about which type of student you are dealing with (English? Science?). You could use objects to improve the clarity of your code; for example, one approach would be to have EnglishStudent and ScienceStudent extend Student and have each calculate their own grades. That should sove most of your problems. Some style tips: You use braces and curly braces much too liberally. You can get away with using much fewer. Also, factor your code. If you have to type the same few lines more than once, you should be doing something differently. Hint: Methods are your friends. Other than that, you should be OK. |
Thank's guys this helped a lot.
|
| All times are GMT -5. The time now is 1:00 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC