Hi i am looking for following output.I am just getting 0 for a,b,c,d,f.
total no of grades
Number of A's
Number of B
Numbers of C
Number of D
number of F

import java.util.Scanner;
public class ExamScores
{
	public static void main (String[]args)
	{
		String a;
		int count= 0;

		int grade=0;
		int A=0;
		int B=0;
		int C=0;
		int D=0;
		int F=0;




	Scanner scan = new Scanner (System.in);
	System.out.println("Enter your exam grades");


	a=scan.nextLine();
	int len=a.length ();

	for (int i=0; i< len; i++)
	{
		if (a.charAt (i) ==' '){
		count--;
	}else {
		count++;

}
}




if (grade > 90 && grade <= 100) {
grade= 'A';
A=A+1;

count++;

} else if (grade > 80 && grade <= 89) {
grade= 'B';
B=B+1;
count++;

} else if (grade > 70 && grade <= 79) {
grade= 'C';
C=C+1;
count++;

} else if (grade > 60 && grade <= 69) {
grade= 'D';
D=D+1;
count++;
} else if (grade> 0 && grade <= 59) {
grade= 'F';
F=F+1;
count++;
}
System.out.println("total no of grades=" + count);
System.out.println("Number of A=" + A);
System.out.println("Number of B=" + B);
System.out.println("Number of C=" + C);
System.out.println("Number of D=" + D);
System.out.println("Number of F=" + F);
}
}

Recommended Answers

All 6 Replies

ehm .. yeah, pretty normal.
you instantiate your variable 'grade' to 0, never change the value yet you do test on this value.
no matter how many times you run it, grade will always be 0

ehm .. yeah, pretty normal.
you instantiate your variable 'grade' to 0, never change the value yet you do test on this value.
no matter how many times you run it, grade will always be 0

so how i change change the grade value.

Ok I put your code in my compiler to see what it was doing. First off they way you have it written the user can only input 1 grade anyway, so no matter what score is entered it will only add one. So your output would only be something like:

total no of grades=1
Number of A=0
Number of B=0
Number of C=0
Number of D=1
Number of F=0

So you need some kind of iteration/loop to actually control how many grades are being entered, for example:

//declare datafields
		String letterGrade;
		double numberOfGrades;
		double grade;
		//create scanner object
		Scanner scan = new Scanner(System.in);
		//ask user for input
		System.out.println("How many grades do you have to enter?: ");
		//reads in the number of grades user wants to enter as double from keyboard
		numberOfGrades = scan.nextDouble();
		//now since program knows how many grades are being entered it can
		//control the input
		for(int j = 1; j <= numberOfGrades; j++){
			//based off of number of grades the user inputs it will ask
			//for that many exam grades
			System.out.println("Enter your exam grades: ");
			//reads in exam scores as a double from keyboard
			grade = scan.nextDouble();
		//compares the datafield grade with scores 90-100	
		if (grade >= 90 && grade <= 100) {
			//if 90 >= grade <=100 the letter grade is A
			letterGrade = "A";
			//if letter grade is A then A which is an int that is initialized at 0
			//has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
			if(letterGrade == "A"){
			A = A + 1;
			}

Also, you should make your datafields(variables) have more meaningful names so your code is easier to read, as I too learned because I did not used to do it either. And use better english for your output, so it looks better for the user, for example you have:

total no of grades=10
Number of A=2
Number of B=2
Number of C=2
Number of D=2
Number of F=2

Try something like this for your println:

System.out.println("Total number of each letter grade: " + numberOfGrades);

Hope all this helped.

Also, you should make your datafields(variables) have more meaningful names so your code is easier to read, as I too learned because I did not used to do it either. And use better english for your output, so it looks better for the user, for example you have:

total no of grades=10
Number of A=2
Number of B=2
Number of C=2
Number of D=2
Number of F=2

Try something like this for your println:

System.out.println("Total number of each letter grade: " + numberOfGrades);

Hope all this helped.

ok i make changes and here r my codes.

import java.util.Scanner;
public class ExamScores
{
	public static void main (String[]args)
	{
		String letterGrade;
				double numberOfGrades;
				double grade;
				//create scanner object
				Scanner scan = new Scanner(System.in);
				//ask user for input
				System.out.println("How many grades do you have to enter?: ");
				//reads in the number of grades user wants to enter as double from keyboard
				numberOfGrades = scan.nextDouble();
				//now since program knows how many grades are being entered it can
				//control the input
				for(int j = 1; j <= numberOfGrades; j++)
					//based off of number of grades the user inputs it will ask
					//for that many exam grades
					System.out.println("Enter your exam grades: ");
					//reads in exam scores as a double from keyboard
					grade = scan.nextDouble();
				//compares the datafield grade with scores 90-100
				if (grade >= 90 && grade <= 100) {
					//if 90 >= grade <=100 the letter grade is A
					letterGrade = "A";
					//if letter grade is A then A which is an int that is initialized at 0
					//has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
					if(letterGrade == "A"){
					A = A + 1;
			}

				if (grade >= 80 && grade <= 89) {
								//if 90 >= grade <=100 the letter grade is A
								letterGrade = "B";}
								//if letter grade is A then A which is an int that is initialized at 0
								//has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
								if(letterGrade == "B"){
								B = B + 1;
			}
				if (grade >= 70 && grade <= 79) {
								//if 90 >= grade <=100 the letter grade is A
								letterGrade = "C";}
								//if letter grade is A then A which is an int that is initialized at 0
								//has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
								if(letterGrade == "C"){
								C = C + 1;
			}
				if (grade >= 60 && grade <= 69) {
								//if 90 >= grade <=100 the letter grade is A
								letterGrade = "D";}
								//if letter grade is A then A which is an int that is initialized at 0
								//has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
								if(letterGrade == "D"){
								D = D + 1;
			}
				if (grade >= 0 && grade <= 59) {
								//if 90 >= grade <=100 the letter grade is A
								letterGrade = "F";}
								//if letter grade is A then A which is an int that is initialized at 0
								//has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
								if(letterGrade == "F"){
								F = F + 1;
							}

			System.out.println("Total number of each letter grade: " + numberOfGrades);
			System.out.println("Number of A" + A);
			System.out.println("Number of B" +B);
			System.out.println("Number of C" + C);
			System.out.println("Number of D" + D);
			System.out.println ("Number of F" + F);


	}


}
}

import java.util.Scanner;
public class ExamScores
{
public static void main (String[]args)
{
String a;
int count= 0;
int A=0;
int B=0;
int C=0;
int D=0;
int F=0;

Scanner input = new Scanner (System.in);
System.out.println("Enter your exam grades");
int grade = input.nextInt();


if (grade > 90 && grade <= 100) {
grade= 'A';
count++;
} else if (grade > 80 && grade <= 89) {
grade= 'B';
B=B+1;
count++;
} else if (grade > 70 && grade <= 79) {
grade= 'C';
C=C+1;
count++;
} else if (grade > 60 && grade <= 69) {
grade= 'D';
D=D+1;
count++;
} else if (grade > 0 && grade <= 59) {
grade= 'F';
F=F+1;
count++;
}
System.out.println("total no of grades=" + count);
System.out.println("Number of A =" + A);
System.out.println("Number of B =" + B);
System.out.println("Number of C =" + C);
System.out.println("Number of D =" + D);
System.out.println("Number of F =" + F);
}
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.