twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: Java Grade Calculator
|
Posted: Mar 20, 2004 6:15 AM
|
|
I won't do your homework for you, but I will help you figure out what you are doing wrong. You are correct that the following code is incorrect. You cannot assign multiple values to a variable. Get rid of this line entirely.
int grade1 = 88 - 100, grade2 = 80 - 87, grade3 = 67 - 79, grade4 = 60 - 67, grade5 = 0 - 60; // not sure if this part is correct
INPUT - PROCESS - OUTPUT You need to do things in that order. The first line below allows the data to be input. The second outputs data. But you haven't processed the data in between.
String inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
//You need code between these two
String message = ("Numeric Grade is: " + inputString + "\nYour Letter Grade Is: " + something + "\n\n" + "To Continue Press Enter. \n"
+ "To exit Press 'x': ");
BTW, as you will see later, you don't want to declare the variables on the same line as you get the data. This is usually a better way to handle things.
String inputString;
//other code
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
Here's what I think that you need to do. I'm sure others might do a few thing differently, but there are multiple ways to solve problems. 1. You need four variables. In addition to the two Strings that you already have, you need something to store the grade in numerical form and something to store the letter grade in.
For the numerical grade, any of the numerical primitives will work, but int is the most likely choice.
For the letter grade, you could use either a char or a String. You don't have to give this variable a value yet.
Declare all of your variables at the beginning of your program BEFORE you do anything else. You do not have to give the Strings any values yet (see example above), but you should give the grade variable an invalid value like -1.
2. Start a loop that checks to see if the numerical grade variable is outside the accepted range. You want to keep the loop going while the variables value is wrong! Put what follows in the body of the loop. This is why you have to declare the variables first. If you don't, they won't be available outside of the loop.
3. Start a try block.
4. In the try block, let the user enter the data. DON'T redeclare your variable!!!!
//wrong
String inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
//right
inputString = JOptionPane.showInputDialog(null, "Enter Grade: ", "Benjamin Shively", JOptionPane.PLAIN_MESSAGE);
3. Use the appropriate wrapper class to convert your String data to numerical data and store that value in your grade variable.
4. Still inside the try block, use an if statement to determine if the grade is inside or outside the acceptable parameters. I'd use the showMessageDialog() method to send a message to the user saying that they entered an invalid number, and asking them to please try again. I would not use the showInputDialog() method to actually try to get the data again yet. Let the loop take care of that.
5. Create the catch block. In it I would probably reset the grade variable to the invalid value to force the loop to continue. Then use the showMessageDialog() method to display a message indicating that the user entered non-numeric data.
6. Close the loop after the end of the catch block. When your code gets this far, you will either have a valid number in the grade variable, which will end the loop, or you will still have an invalid value (like -1), so the loop will repeat and ask the user to enter a grade.
7. After the loop is finished, use a series of if-else statements to determine what range the numerical grade falls in. Set the letter grade in the various blocks. Be sure to use else on the last choice so the letter grade variable get set to something.
if(A condition)
letterGrade = "A";
else if(B condition)
//other code
else
letterGrade = "F";
A switch would also work, but I like to avoid the switch statement.
8. Modify your output code to include the letter grade variable and you should be good to go.
I hope this helps. twc
|
|