java program help needed.

Discussion in 'Software' started by summer05storms, Feb 27, 2005.

  1. summer05storms

    summer05storms Private E-2

    I am trying to create a java program that will allow the user to enter exam grades and then give me a total count of how many exam grades I enter then give me a count of how many A's, B's, C's, D's and F's there are. Here is what I have:
    Also at this time when I compile and run it lets me put in only 2 grades and tells me there is 2 but then ask me if I want to enter in more or not.
    Thanks,
    Summer

    public class CountletterGrades
    {
    public static void main (String [] args)
    {
    int examGrade = 00;
    int count = 1;
    char letterGrade;
    char answer;


    do
    {
    System.out.println("Enter list of exam grades end list with a negaitve number: ");
    examGrade = SavitchIn.readLineNonwhiteChar ();
    examGrade = SavitchIn.readLineInt();

    if ((examGrade >=90)||(examGrade<=100))
    {
    letterGrade = 'A';
    count++;
    System.out.print(count + ", ");
    }
    else if ((examGrade >=80)||(examGrade<=89))
    {
    letterGrade = 'B';
    count++;
    System.out.print(count + ", ");
    }
    else if((examGrade >=70)||(examGrade<=79))
    {
    letterGrade = 'C';
    count++;
    System.out.print(count + ", ");
    }
    else if ((examGrade >=60)||(examGrade<=69))
    {
    letterGrade = 'D';
    count++;
    System.out.print(count + ", ");
    }
    else if ((examGrade >=0)||(examGrade<=59))
    {
    letterGrade = 'F';
    count++;
    System.out.print(count + ", ");
    }

    System.out.println("Want to enter more exams?");
    System.out.println("Enter y for yes or n for no.");
    answer = SavitchIn.readLineNonwhiteChar();
    }while ((answer == 'y')||(answer == 'Y'));

    }
    }
     
  2. summer05storms

    summer05storms Private E-2

    ALso, I have yahoo messenger....summer05storms if you have it also and want to IM me.

    thanks,
    Summer
     
  3. QuickSilver

    QuickSilver Corporal

    Some things that stick out to me :

    1) - The way you are incrementing count seems a little strange - you have the line 'count++;' in multiple times when the code is only ever going to hit that branch once per iteration.
    Would this be better?

    Code:
         int examGrade = 00;
        int count = 0;
        char letterGrade;
        char answer;
    
    
        do
        {
           count++;
          System.out.println("Enter list of exam grades end list with a negaitve number: ");
          examGrade = SavitchIn.readLineNonwhiteChar (); 
          examGrade = SavitchIn.readLineInt();
    This way you initialise the variable to 0 and then always increment it each time the do-while loops.


    2) - This bit seems odd :

    Code:
          examGrade = SavitchIn.readLineNonwhiteChar (); 
          examGrade = SavitchIn.readLineInt();
    The reason being you are saving the results of a method call into variable examGrade and then overwriting it without ever using it.
    Which method do you want to use?
    Is the SavitchIn a private class you are using? I haven't come across it before - I couldn't check what those different methods do...

    3) - Other than that the only thing I can suggest you do is add debug to the code. Put numerous println type statements in that will otuput the value of variables as you go so you can see how they change. Pay particular attention to the contents of the variable answer as this is what is the basis fo the loop.

    Hope some of this helps.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds