My assignment is to create a program that accepts a list of exam scores (ranges from 0-100) and output the total number of grades and then the number of each in letter grade..
but my problem in the beginning of doing my program is:
public class Grades {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int grade= 0;
int nega= 0;
do{
grade=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Grade:"));
}while (grade >0);
if (grade>0)
{
JOptionPane.showMessageDialog(null,"The Input Numbers are:"+ grade+"");
}
}
}
instead of the grades the i have inputted should be the output, the negative number that i have inputted is the one who's become my output, the negative number should serve as my sentinal value, it should not show on the output.
please help thanks
well your grades are only being saved one at a time in the do while loop and once the do while loop has ended.. which you would haave to type in a negative then only will the execution stop and your next line is if(grade>0) so i dont understand how this is working. shouldnt you initialize your values in an array of size 100 and wait after 100 grades are entered by keeping a counter of how many are entered and adding them to the array of grades respectively, then only can you get all your output.
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
yes.
you want for each letter (A, B, C) how many times a grade has been entered, so:
int total = total+grade;
if ( grade == AValue ) a+= 1;
else if ( grade == BValue ) b+= 1;
a bit like the above suggestion would do
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
inside. you only have the value of each grade itself within that if-statement
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
does that mean that i will have to make an "if-else inside the do while statement, or outside?
yeah stultuske does have a good way though... but is this just going to be used to output the grades or will you it to preform individual calculations on the marks?
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
both!!
this is the sample :
(our teacher gave us this:
90-100= A
80-89= B
70-79 =C
60-69=d
0=59=F)
i will enter grades:
98
87
86
87
98
-1
-once i have reached that -1,it will stop accepting grade and will show this window:
The input NUmbers are: 98 87 86 87 98
The total number of grades: 5
NUmber of A's:2
Number of B's:3
..something like that..
:)
Oh okay no then all is fine and dandy :)
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
thank you i followed the code that u instructed and i think that its right but i still dont have the codes for the Avalue, Bvalue Cvalue.. and this is what i am thinking about to to:
if (grade >=90 )
{
Agrade+=grade;
}
but..i think its wrong because the grade is greater than 90 and equals to one hundred but i dont know how to do that
then you would use the && operator like this:
if(grade>90&&grade<=100) {
//increment counter for all A values-to show at the end how many A's B's etc, print out the mark etc
}
The codes for A B values etc which just need 5 counters which will be incremented in the appropriate if statement i.e the A grade value counter will be incremented in you if statement where you check if the mark is 90 and greater but less then or equal too 100
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
why'd u edited the message im trying to read it over and over again so that i can get some ideas.. :( haha
Thought the information i had given wasnt really understandable :P
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
no .. everything that u have written here are very helpful to me, i just need to do it little by little since im a beginner i did what u told me:
do{
grade=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Grade:"));
if (grade >=90 && grade <=100 )
{
Agrade+=grade;
}
else if (grade >=80 && grade <=89 )
{
Bgrade+=grade;
}
}while (grade >0);
JOptionPane.showMessageDialog(null,"The Input Numbers are:"+ Agrade+""+""+Bgrade);
but why is that the grades that i have inputted are not the ones that are showing, its a different thing.. :(
Look at stultuske's post. what you are adding now is the actual grade mark i.e 80 out of 100. what you want to be adding is 1. just to increment(increase) the counter each time a certain branch of the if statement is taken thus showing you the amount of times there was an A grade B grades etc. You would print the number inputed here:
grade=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Grade:"));
System.out.println("Grade: "+ grade);//will print each grade
if (grade >=90 && grade <=100 )
{
Agrade+=1;
}
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
does that mean that i will have to do something like... a for loop?
something like this
for (x=1;x>=100;x++
something like that?
see my edited post above
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
yes thts wight, but im using JOptionPane.showMessage, and my output should not show one at a time, all of them should come out in one time only :)
now thats unfortunately where arrays are awesome but you are not allowed? so you'll have to declare values for all marks up to 100 i.e mark1,mark2 etc
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169