Hey guys =] First I just wanted to say I am excited to have found this forum, it looks like the members are very helpful and friendly! Hopefully I'll be able to contribute what I can but I'm not very knowledgeable :P
Anyway, I am to create a grading program(utilizing only loops, arrays aren't allowed) that accepts grades input by the user, quits when they enter the sentinel value, and display the highest and lowest grades. Also the teacher would like us to display the mean, median and mode if possible (I assume that the median and mode aren't possible without an array). All this was easy and I got it done without cheating from the book like the rest of my classmates :D.
I decided to add some of my own touches to it, like displaying the number of each equivalent letter grade, the number of passing and failing grades, and the percentage of each. This is where my problem comes in! I can't figure out how to create percentages!!
For example:
There is a counter for the total valid grades entered, as well as seperate counters for 'As', 'Bs', 'Cs', etc. If I try to find the percent of A grades out of the total I just can't figure it out. I messed around trying different things for 2 hours or so and tried googling for a class I could import or an equation that could help me. I know what I have now is incorrect, I just can't figure out why. This logic seems right to me... percent = ((part/total)*100). It works like that in a calculator. Does anyone have any advice to help me? Is there something I'm just looking over?
I'm not here to get answers without trying, I just really need a push in the right direction from somebody who knows what they're talking about(because I sure don't). I've tried really hard to solve the problem myself and this is a last resort! Also, if you can't help with the percents problem, could you at least critique me on the style of my coding (neatness/efficiency), that would be very very helpful as well :) I'm trying to improve myself! Thanks guys!!!!!!!!!! :icon_cheesygrin:
Oh! and out of curiousity... is it possible to make a String my sentinel value even though the input is a double type? (User types 'quit' instead of -1) Hmm we did discuss data type conversions a bit I'll look into it if its possible :)
import java.util.*;
public class Grades {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Variables
double grade = 0;
double sum = 0;
double highest = 0;
double lowest = 100;
int counter = 0;
int Acounter = 0;
int Bcounter = 0;
int Ccounter = 0;
int Dcounter = 0;
int Fcounter = 0;
double Apercent = 0;
double Bpercent = 0;
double Cpercent = 0;
double Dpercent = 0;
double Fpercent = 0;
//Header
System.out.println("\t\t\t\tGrades Program");
System.out.println("\nEnter valid student grades (in decimal values ranging from 0-100)." +
"\nInvalid grades will not be considered. Enter -1 to view the results.\n\n");
//Scope of valid input numbers
if (grade>=0 && grade<=100){
while ( grade != -1 ){ //sentinel value
System.out.print("\tInput a grade: ");
grade = input.nextDouble();
//Determines # of valid grades and # of each letter grade
if (grade<=100 && grade>=93){
Acounter++;
counter++;
sum = sum+grade;
}
else if (grade<=92 && grade>=85){
Bcounter++;
counter++;
sum = sum+grade;
}
else if (grade<=84 && grade>=75){
Ccounter++;
counter++;
sum = sum+grade;
}
else if (grade<=75 && grade>=70){
Dcounter++;
counter++;
sum = sum+grade;
}
else if (grade<=74 && grade>=0){
Fcounter++;
counter++;
sum = sum+grade;
}
//Determines highest and lowest values
if (highest<grade){
highest=grade;
}
if (lowest>grade){
lowest=grade;
}
}
}
//Calculations
Apercent = ((Acounter/counter)*100);
Bpercent = ((Bcounter/counter)*100);
Cpercent = ((Ccounter/counter)*100);
Dpercent = ((Dcounter/counter)*100);
Fpercent = ((Fcounter/counter)*100);
int pass = (Acounter+Bcounter+Ccounter+Dcounter);
double passPercent = ((pass/counter)*100);
double average = (sum/counter);
//Output
System.out.println("\n\nYour Results:");
System.out.println("\n\nThe total amount of valid grades input were: " + counter);
System.out.println("\nLetter Equivalent\tInstances\t% of total\t");
System.out.println("\tAs:\t\t" + Acounter + "\t\t" + Apercent + "%");
System.out.println("\tBs:\t\t" + Bcounter + "\t\t" + Bpercent + "%");
System.out.println("\tCs:\t\t" + Ccounter + "\t\t" + Cpercent + "%");
System.out.println("\tDs:\t\t" + Dcounter + "\t\t" + Dpercent + "%");
System.out.println("\tFs:\t\t" + Fcounter + "\t\t" + Fpercent + "%");
System.out.println("\n\tpass:\t\t" + pass + "\t\t" + passPercent + "%");
System.out.println("\tfail:\t\t" + Fcounter + "\t\t" + Fpercent + "%");
System.out.println("\nThe highest valid grade you entered was: " + highest);
System.out.println("The lowest valid grade you entered was: " + lowest);
System.out.println("\nThe average of the valid grades you entered is: " + average + "\n\n");
}
}