Results 1 to 11 of 11
- 04-27-2010, 12:39 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
not sure what im doing wrong with these if else statements
I am having problems with this. this is supposed to give you a list of things. the full code is this
for some reason this statement does not work properly. the first part works but then the second part does notJava Code:package students; import javax.swing.JOptionPane; public class Students { public static void main(String[] args) { //first list the integers and strings which are //the student’s major (CIS or Math) //The student’s last name //The number of credits the student has completed //The gpa //The tuition paid int tuition, finish = 1, students=0, credits, major, credits1 = 0, credits2 = 0, credits3 = 0, credits4 = 0; String smajor, last, sgpa, stuition, sfinish, scredits; double largestg = 0, largestm1 = 0, largestm2 = 0, gpa, totalgpa=0, totaltuition=0; String highnameg = "", highnamem1 = "", highnamem2 = ""; int totalcredits=0; while (finish != 0) { students=students+1; smajor = JOptionPane.showInputDialog(null, "Enter students major (1 for math 2 for cis)", "Input Data", JOptionPane.QUESTION_MESSAGE); last = JOptionPane.showInputDialog(null, "Enter students last name", "Input Data", JOptionPane.QUESTION_MESSAGE); scredits = JOptionPane.showInputDialog(null, "Enter students total amount of credits", "Input Data", JOptionPane.QUESTION_MESSAGE); sgpa = JOptionPane.showInputDialog(null, "Enter students GPA", "Input Data", JOptionPane.QUESTION_MESSAGE); stuition = JOptionPane.showInputDialog(null, "Enter students tuition paid", "Input Data", JOptionPane.QUESTION_MESSAGE); credits = Integer.parseInt(scredits); gpa = Double.parseDouble(sgpa); tuition = Integer.parseInt(stuition); major = Integer.parseInt(smajor); totalgpa=totalgpa+gpa; totaltuition=totaltuition+tuition; totalcredits=totalcredits+credits; if (gpa > largestg) { largestg = gpa; highnameg = last; } if (major == 1) { if (gpa > largestm1) { largestm1 = gpa; highnamem1 = last; } else if (major == 2) { if (gpa > largestm2) { largestm2 = gpa; highnamem2 = last; } } if (credits <= 30) { credits1 = students; } else if (credits >=31 && credits <=60) { credits2 = students; } else if (credits >=61 && credits <=90) { credits3 = students; } else if (credits >=90) { credits4 = students; } } sfinish = JOptionPane.showInputDialog(null, "Enter 0 if your done or 1 to continue", "Input Data", JOptionPane.QUESTION_MESSAGE); finish = Integer.parseInt(sfinish); } //while loop //do calcs and such double agpa = totalgpa / students; double avgtu = totaltuition / students; int avgcr = totalcredits / students; //print out results JOptionPane.showMessageDialog(null, "\nAverage GPA = " + agpa + "\nTuition = $" + totaltuition + "\nAverage tuition = $" + avgtu + "\nAverage credits = $" + avgcr + "\nHighest GPA from " + highnameg +" " +largestg + "\nHighest GPA from a math student is " + highnamem1 +" from " +largestm1+ "\nHighest GPA from a cis student is " + highnamem2 +" from " +largestm2+ "\nNumber of Freshman is " +credits1+ "\nNumber of Sophomore is " +credits2+ "\nNumber of Junior is " +credits3+ "\nNumber of Senior is " +credits4+ JOptionPane.INFORMATION_MESSAGE); } }
Java Code:if (major == 1) { if (gpa > largestm1) { largestm1 = gpa; highnamem1 = last; } else if (major == 2) { if (gpa > largestm2) { largestm2 = gpa; highnamem2 = last; } } .......... JOptionPane.showMessageDialog(null, "\nAverage GPA = " + agpa + "\nTuition = $" + totaltuition + "\nAverage tuition = $" + avgtu + "\nAverage credits = $" + avgcr + "\nHighest GPA from " + highnameg +" " +largestg + "\nHighest GPA from a math student is " + highnamem1 +" from " +largestm1+ "\nHighest GPA from a cis student is " + highnamem2 +" from " +largestm2+
and the next one wont work at all
I appreciate any help you can giveJava Code:if (credits <= 30) { credits1 = students; } else if (credits >=31 && credits <=60) { credits2 = students; } else if (credits >=61 && credits <=90) { credits3 = students; } else if (credits >=90) { credits4 = students; } } ....... "\nNumber of Freshman is " +credits1+ "\nNumber of Sophomore is " +credits2+ "\nNumber of Junior is " +credits3+ "\nNumber of Senior is " +credits4+ JOptionPane.INFORMATION_MESSAGE);Last edited by yrvd86; 04-27-2010 at 01:14 AM. Reason: done incorrectly
- 04-27-2010, 12:59 AM #2
Welcome to the Forum
please try to make it easier for someone to help you
first you should edit your post and repaste your code using code tags
see: Java Forums - BB Code List
exactly why is the code not working? state what it should return and state what it is actually returning
see: How To Ask Questions The Smart Way:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 04-27-2010, 02:05 AM #3
i cant see any problems, although I hasten to add i'm not an expert.]
there are some spacing indentation and coding style issues that make your code difficult to read.
perhaps your editor can fix the spacing and indentation for you
if you use Eclipse 'select all' (ctrl a)
then on the 'source' dropdown menu there are 'format' command and 'correct indentation' command.
you say the code does not work but you don't say why exactly.
what does the code do wrong. what are you expecting to get in the final list compared with what are you actually getting.Last edited by sonny; 04-27-2010 at 02:09 AM.
:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 04-27-2010, 02:20 AM #4
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
depending on if you type one or two for major it should show how many students have that major plus their gpa. The first one works which would be this
but then the next part for 2 will not show any results in the endJava Code:if (major == 1) { if (gpa > largestm1) { largestm1 = gpa; highnamem1 = last; }
Then the statments for the credits wont show the results for freshman through seniorJava Code:else if (major == 2) { if (gpa > largestm2) { largestm2 = gpa; highnamem2 = last; } }
This doesnt really make sense to me since the first one works fine. im not sure if there is a continuity error or what but it will not work. Thanks againJava Code:if (credits <= 30) { credits1 = students; } else if (credits >= 31 && credits <= 60) { credits2 = students; } else if (credits >= 61 && credits <= 90) { credits3 = students; } else if (credits >= 90) { credits4 = students; } }
- 04-27-2010, 02:33 AM #5
the reason why it is so important to indent and space code correctly is so that you quckly find the things that are wrong
take a look at your curly braces...
does the if else ever get evaluated to be true?
Java Code:if (major == 1) [COLOR="Red"]{[/COLOR] if (gpa > largestm1) [COLOR="Red"]{[/COLOR] largestm1 = gpa; highnamem1 = last; [COLOR="Red"]}[/COLOR] else if (major == 2) { if (gpa > largestm2) { largestm2 = gpa; highnamem2 = last; } } if (credits <= 30) { credits1 = students; } else if (credits >= 31 && credits <= 60) { credits2 = students; } else if (credits >= 61 && credits <= 90) { credits3 = students; } else if (credits >= 90) { credits4 = students; } }:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 04-27-2010, 02:58 AM #6
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
So then what should I change on it? I honestly dont see the error
- 04-27-2010, 03:22 AM #7
okay your code says this
the colour changes as your blocks of code change
the code before this is green
if major == 1
open curly brace
do this next block of code
open curly brace
if gpa > ...... etc
close curly brace....
so were done with the purple block and it is closed so we back with the blue block
then it says if major == 2,, how can major be == 2, were are still in the blue block.
the blue block of code is executed because major == 1, if major == 1 it cant == 2
count your curly braces, how many have you opened, how many have you closed??
does that make sense. post straight back if not:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 04-27-2010, 03:37 AM #8
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
thanks alot I really appreciate your help it worked.
-
This confuses me too:
I'm guessing that here if the student has less then 30 credits, increase the count of freshmen by 1 (credits1), else if they have 31 to 60 credits, increase the number of sophomores (credits2) by 1, etc...Java Code:if (credits <= 30) { credits1 = students; } else if (credits >= 31 && credits <= 60) { credits2 = students; } else if (credits >= 61 && credits <= 90) { credits3 = students; } else if (credits >= 90) { credits4 = students; } }
If so, you are gaining nothing by setting credits1 or 2 or 3 or 4 to the total number of students at that time as this logically doesn't make sense. More logical (in my limited understanding of your problem) is to instead increment credits1 or credits2 or credits3 or credits3 depending on the value of credits. In other words, something like:
If I'm totally missing your problem, then please ignore this recommendation, but please let me know.Java Code:if (credits <= 30) { credits1++; } else if (credits >= 31 && credits <= 60) { credits2++; } else if (credits >= 61 && credits <= 90) { credits3++; } else if (credits >= 90) { credits4++; } }
- 04-27-2010, 03:48 AM #10
you still have another curly brace issue to solve to get your code to work
and it is here
look at your whole while loop,, how many have you opened, and how many have you closed???Java Code:else if (credits >= 90) { credits4 = totalStudents; } [COLOR="Red"]}[/COLOR]
which block of code is controlling things.. (possibly not the best way of explaining it)
i think you need to remove the red curly brace and it all should work,:D
EDIT hadnt seen fubarables post, and was still just looking at curly bracesLast edited by sonny; 04-27-2010 at 03:50 AM.
:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 04-27-2010, 02:07 PM #11
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
if else statements
By sweetpea123 in forum New To JavaReplies: 4Last Post: 04-12-2010, 07:02 PM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
age: using if statements
By yasmin k in forum New To JavaReplies: 2Last Post: 10-04-2009, 09:50 PM -
Help with if-else statements
By porchrat in forum New To JavaReplies: 4Last Post: 03-23-2009, 04:24 PM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 07:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks