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");
	}
}

Hey, I think you are on the right track with your percentages. So by it not working, I assume you get getting results of 0% for everything?

If this is indeed the case, the problem is int / int is integer division, and will return a int. So... since counter is always greater than your percentage, it is always 0.XYZ which will get trimmed to be 0. Since you are keen to learn and put in the effort, you should be able to find a solution to this :).

In regard to having string inputs, sure you will be able to do this. But if you decide to take this route, you may need to learn some exception handling... I am not too sure how strict your requirements for the assignment are and it seems you are already going beyond the scope, so perhaps you won't need exception handling.

hope I have helped...

thank you sillyboy I appreciate you taking the time to help :)

yes I get 0.0% for everything >.< I'll think about what you said and play around with the code a bit more :)

double Apercent = (((double)Acounter)/counter)*100); I tried changing my percentage equations to this... I read that the Acounter will be considered a double now as well as the counter if used like that but no luck

That looks correct, so if it isn't working I fear you probably have some silly mistake somewhere else :p

This is a simple test program I wrote:

public class Main {
	
	public static void main(String [] args) {
		
		double result;
		
		int value = 50;
		int count = 100;
		
		result = ((double)value / count) * 100;
		
		System.out.println(result);
	}

}

Console output:

50.0

I copy / pasted your original code and added (double) in the divisions, this is the output I got (looks like it works to me):

Grades Program

Enter valid student grades (in decimal values ranging from 0-100).
Invalid grades will not be considered. Enter -1 to view the results.


	Input a grade: 99
	Input a grade: 85
	Input a grade: 70
	Input a grade: 60
	Input a grade: 50
	Input a grade: 40
	Input a grade: 30
	Input a grade: -1


Your Results:


The total amount of valid grades input were: 7

Letter Equivalent	Instances	% of total	
	As:		1		14.285714285714285%
	Bs:		1		14.285714285714285%
	Cs:		0		0.0%
	Ds:		1		14.285714285714285%
	Fs:		4		57.14285714285714%

	pass:		3		42.857142857142854%
	fail:		4		57.14285714285714%

The highest valid grade you entered was: 99.0
The lowest valid grade you entered was: -1.0

The average of the valid grades you entered is: 62.0

looks like lowest grade needs to be changed a little bit

if ((lowest>grade)  && (grade > -1)){
     lowest=grade;	
}

Hmm I tried copy pasting exactly what I posted here (to make sure I didn't alter something since then) and re-added the (double) and it still doesn't work >.<

Apercent = (((double)Acounter/counter)*100);
	Bpercent = (((double)Bcounter/counter)*100);
	Cpercent = (((double)Ccounter/counter)*100);
	Dpercent = (((double)Dcounter/counter)*100);
	Fpercent = (((double)Fcounter/counter)*100);
		
	int pass = (Acounter+Bcounter+Ccounter+Dcounter);
	double passPercent = (((double)pass/counter)*100);

Dickersonka, I added an if statement before the loop so that anything input not between 0 and 100 would go uncounted =]

read closely at my previous post

notice 100.0

Apercent = (((double)Acounter/counter)*100.0);

i know you have an if statement before the loop, the problem is that you are reading inside the loop for it to terminate, thats why you need that check in there

grade = input.nextDouble();

that is inside the loop and won't hit your if check

while ( grade != -1 && (grade>=0 && grade<=100)){ hmm either way seems to work, but is that okay?

Also I tried:

//Calculations 	
	double Apercent = (((double)Acounter/counter)*100.0);
	double Bpercent = (((double)Bcounter/counter)*100.0);
	double Cpercent = (((double)Ccounter/counter)*100.0);
	double Dpercent = (((double)Dcounter/counter)*100.0);
	double Fpercent = (((double)Fcounter/counter)*100.0);

and still does not work :\

hmmm, i have the same code and it is working for me

this piece is above the while loop

if (grade>=0 && grade<=100)

the piece of code i meant

if (highest<grade){
				highest=grade;
				}
				if (lowest>grade){
				lowest=grade;	
				}

needs the bottom if to be changed to

if (highest<grade){
highest=grade;
}
if ((lowest>grade)  && (grade > -1)){
     lowest=grade;	
}

Yes I get 0.0% for everything unless they're all As I get 100% for As and 100% for pass or Bs 100% for Bs and 100% for pass...... or same for Fs and fail

From what I've read it is working for everybody except you...:(

What IDE are you using to program, compile, run this code? Can you give some details on your inputs etc...

Send me you code if you want and I will try it without touching it at all.

Ah after redownloading JCreator FOUR times, I got the code to work :) Although it took me forever to get it working (apparently due to what I was using to run it and not the code itself) I ended up tweaking it a lot and improving it so... all for the better :D

This question has already been answered. Start a new discussion instead.