Results 1 to 9 of 9
Thread: need help to fix my errors
- 11-15-2011, 11:14 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
need help to fix my errors
Hi i am looking for following output.so far I am only getting result for total no of grades.I am getting 0 for A B C D and F.
total no of grades
Number of A's
Number of B
Numbers of C
Number of D
number of F
Java Code:import java.util.Scanner; public class ExamScores1 { public static void main (String[]args) { String a; int count= 0; int grade=0; // you declare grade = 0 here 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++; } } // grade is still == 0 here if (grade > 90 && grade <= 100) { grade= 'A'; A++; count++; } else if (grade > 80 && grade <= 89) { grade= 'B'; B++; count++; } else if (grade > 70 && grade <= 79) { grade= 'C'; C++; count++; } else if (grade > 60 && grade <= 69) { grade= 'D'; D++; count++; } else if (grade> 0 && grade <= 59) { grade= 'F'; F++; 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); } }
-
Re: need help to fix my errors
You're not doing your large if/else block inside of the for loop and so it's being called only once, and grade isn't even set when it's called. I would:
Put that code inside of the loop, and get the numeric value of each grade in the for loop. You will probably need to use a while loop not a for loop and use your Scanner to scan through the entered String and to pull out the grade ints. By the way, what is the form of their input? Should you tell the user how exactly they should enter the grades?
- 11-15-2011, 11:35 PM #3
Re: need help to fix my errors
Let's take a look at your code. Imagine that grade was 95. That makes the if statement true and enters that branch. The very next thing it does is set grade the the char 'A'. What is the point of that?Java Code:if (grade > 90 && grade <= 100) { grade= 'A';
- 11-16-2011, 12:06 AM #4
Re: need help to fix my errors
Scanner is a class in the Java API. If you are unfamiliar with it I suggest looking it up in the API. In short it a class that is used to gather user input.
-
Re: need help to fix my errors
- 11-16-2011, 12:47 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
- 11-16-2011, 12:50 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: need help to fix my errors
can u plz explain me how to do that. i would tell user to enter his grades.so when user will enter numeric grades. i will get no of a,b,c,d,f he got.
- 11-16-2011, 01:18 AM #8
- 11-17-2011, 06:19 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
Help with three errors -.-
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 03-30-2010, 06:52 PM -
Errors,errors and errors
By xpatiencex in forum New To JavaReplies: 6Last Post: 04-25-2009, 04:43 AM -
Errors.
By rocky in forum New To JavaReplies: 4Last Post: 04-09-2009, 08:05 AM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks