I got everything working until I decided it was time to start writing methods.
I just need help with the method I wrote below.
It works fine, but the aCount++, bCount++, etc, are not working. At the end of the program their values remain 0, although everything else works.
public static void gradeLetter(float grade, StringBuffer gradeList, int aCount, int bCount, int cCount, int dCount, int fCount)
{
if (grade > 100)
{
System.out.println("Error! Please enter only grades up to 100. Restart the program to continue.");
System.exit(0);
}
if (grade >= 90 && grade <= 100)
{
gradeList.append(g + ": A\n");
aCount++;
return;
}
if (grade >= 80 && grade <= 89)
{
gradeList.append(g + ": B\n");
bCount++;
return;
}
if (grade >= 70 && grade <= 79)
{
gradeList.append(g + ": C\n");
cCount++;
return;
}
if (grade >= 60 && grade <= 69)
{
gradeList.append(g + ": D\n");
dCount++;
return;
}
if (grade >= 0 && grade <= 59)
{
gradeList.append(g + ": F \n");
fCount++;
return;
}
}
And here's the entire code, just in case.
package assignement;
import java.util.*;
public class Program3
{
public static void main(String[] args)
{
StringBuffer gradeList = new StringBuffer (""); //list of grades empty for now
int count = 2; //keeps track of the amount of grades the user has entered
float sum = 0; //the sum of all grades to calculate the average
float next1; //variable used in the do-white loop
int aCount = 0; //these are used to keep track of the amount of
int bCount = 0; //each letter grade. It is used to calculate the
int cCount = 0; //percentage of each.
int dCount = 0;
int fCount = 0;
System.out.println("Enter at least 2 grades. Enter a negative number to signal finish: ");
Scanner keyboard = new Scanner(System.in);
float grade = keyboard.nextFloat();
float next = keyboard.nextFloat();
float max = grade; //at this point, min and max are assigned the value of grade
float min = grade;
gradeLetter(grade, gradeList, aCount, bCount, cCount, dCount, fCount);
gradeLetter(next, gradeList, aCount, bCount, cCount, dCount, fCount);
sum = grade + next;
if (next > max)
{
max = next;
}
if (next < min)
{
min = next;
}
do
{
next1 = keyboard.nextFloat();
if (next1 < 0)
{
break; //ends loop when user enters a negative number
}
if (next1 > max)
{
max = next1;
}
if (next1 < min)
{
min = next1;
}
gradeLetter(next1, gradeList, aCount, bCount, cCount, dCount, fCount);
sum = sum + next1; //updates value of sum to include new value of next1
count ++; //if loop was successful, count gets raised
} while (next1 >= 0);
float aPrc = (aCount / count) * 100;
float bPrc = (bCount / count) * 100;
float cPrc = (cCount / count) * 100;
float dPrc = (dCount / count) * 100;
float fPrc = (fCount / count) * 100;
float average = (sum / count);
System.out.println("You have entered " + count + " grades.");
System.out.println("Average: " + Math.round(average));
System.out.println("The highest score is " + max);
System.out.println("The lowest score is " + min);
System.out.println("The letter grades for each grade are: \n" + gradeList);
System.out.println("The percentage of of each letter grade category are:");
System.out.println("A: " + Math.round(aPrc) + "%");
System.out.println("B: " + Math.round(bPrc) + "%");
System.out.println("C: " + Math.round(cPrc) + "%");
System.out.println("D: " + Math.round(dPrc) + "%");
System.out.println("F: " + Math.round(fPrc) + "%");
}
public static void gradeLetter(float g, StringBuffer gradeList, int aCount, int bCount, int cCount, int dCount, int fCount)
{
if (g > 100)
{
System.out.println("Error! Please enter only grades up to 100. Restart the program to continue.");
System.exit(0);
}
if (g >= 90 && g <= 100)
{
gradeList.append(g + ": A\n");
aCount++;
return;
}
if (g >= 80 && g <= 89)
{
gradeList.append(g + ": B\n");
bCount++;
return;
}
if (g >= 70 && g <= 79)
{
gradeList.append(g + ": C\n");
cCount++;
return;
}
if (g >= 60 && g <= 69)
{
gradeList.append(g + ": D\n");
dCount++;
return;
}
if (g >= 0 && g <= 59)
{
gradeList.append(g + ": F \n");
fCount++;
return;
}
}
}
Here's the output I'm getting:
Enter at least 2 grades. Enter a negative number to signal finish:
96
95
81
56
74
23
-1
You have entered 6 grades.
Average: 71
The highest score is 96.0
The lowest score is 23.0
The letter grades for each grade are:
96.0: A
95.0: A
81.0: B
56.0: F
74.0: C
23.0: F
The percentage of of each letter grade category are:
A: 0%
B: 0%
C: 0%
D: 0%
F: 0%
This post has been edited by carnivroar: 18 September 2011 - 06:47 PM

New Topic/Question
Reply



MultiQuote





|