Results 1 to 11 of 11
  1. #1
    MAJ
    MAJ is offline Member
    Join Date
    Jul 2014
    Posts
    17
    Rep Power
    0

    Default Professor's code doesn't work?

    My professor gave a worksheet with this code that doesn't seem to produce the intended result. I don't know what's missing.
    import java.util.*;
    public class gradeAverage
    {
    public static void main (String[] args)
    {
    int[]grade = new int[100];
    int numExams , sum = 0;
    int changeIndex , newGrade;
    Scanner getData = new Scanner(System.in);
    System.out.println("How many exams?");
    numExams = getData.nextInt();
    for (int i = 1 ; i<= numExams ; i++)
    {
    System.out.println("Grade on test " + i + " ? ");
    grade[i] = getData.nextInt();
    sum = sum + grade[i] ;

    System.out.println("Your average is:" + (sum/numExams));
    for (int j = 1 ; j<= numExams ; j++)
    {
    System.out.println("grade # " + j+ " was a " + grade[j]);
    }
    System.out.println("Which grade to change?");
    changeIndex = getData.nextInt();
    System.out.println("What do you want to change it to?");
    newGrade = getData.nextInt();
    grade[changeIndex] = newGrade;
    sum = 0;
    for (int j = 1 ; j<=numExams ; j++)
    {
    System.out.println("grade # " + j + " was a " + grade[j]);
    sum = sum + grade[j] ;
    }
    System.out.println("Your average is: " + (sum/numExams));
    }
    }
    }
    The result is
    How many exams?
    3
    Grade on test 1 ?
    80
    Your average is:26
    grade # 1 was a 80
    grade # 2 was a 0
    grade # 3 was a 0
    Which grade to change?

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default Re: Professor's code doesn't work?

    what result u expect first? you should know the purpose of this code for inpuuting the data.
    code can compile but what input it expects??????
    Ramya:cool:

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Professor's code doesn't work?

    When posting code please wrap it in [ code] tags [ /code] so it retains its formatting.
    It's hard to follow the flow of code without seeing the indentation.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Professor's code doesn't work?

    I'm not replying to this, it looks too much like the whole assignment is to fix this code so it DOES produce the correct output.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    MAJ
    MAJ is offline Member
    Join Date
    Jul 2014
    Posts
    17
    Rep Power
    0

    Default Re: Professor's code doesn't work?

    Nope.The assignment for this is to practice how to write this code for Thursday's exam.

  6. #6
    MAJ
    MAJ is offline Member
    Join Date
    Jul 2014
    Posts
    17
    Rep Power
    0

    Default Re: Professor's code doesn't work?

    I was expecting the program to ask me what I got on the second and third test so then I can type my input.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Professor's code doesn't work?

    Quote Originally Posted by MAJ View Post
    Nope.The assignment for this is to practice how to write this code for Thursday's exam.
    Your exam involves regurgitating memorised code?

    Blimey...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Professor's code doesn't work?

    The problem here is remarkably simple. You need to do the following steps.

    If you use Eclipse:
    1.) Create a new file in Eclipse.
    2.) Paste your code in.
    3.) Press ctrl+shit+f

    Or if you use Dr. Java:
    1.) Paste your code into a new file.
    2.) Press ctrl+a to select all.
    3.) Press tab

    If you use anything else, just figure out how to autoindent. Your whole issue is the result of a misplaced brace, but you need to figure that out for yourself. Getting your code indented is the first step, and it'll help you find the issue. Walk through the code in your head before posting on a forum for such a simple issue.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Professor's code doesn't work?

    Quote Originally Posted by AlexGraal View Post
    The problem here is remarkably simple. You need to do the following steps.
    3.) Press ctrl+shit+f
    Ah, the Freudian slip or the subliminal message. Either way, it made my day!

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Professor's code doesn't work?

    Quote Originally Posted by jim829 View Post
    Ah, the Freudian slip or the subliminal message. Either way, it made my day!

    Regards,
    Jim
    And what a day it is. I'm flipping from nuts to full-on mentally broken down.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Professor's code doesn't work?

    Wow I didn't even catch that. I think I'll leave it as is though.

Similar Threads

  1. Replies: 13
    Last Post: 10-04-2012, 08:45 PM
  2. Why doesn't this code work?
    By abv in forum New To Java
    Replies: 3
    Last Post: 08-07-2012, 01:32 PM
  3. My code doesn't work! Pane.showOptionDialog()
    By 0070071 in forum AWT / Swing
    Replies: 5
    Last Post: 01-18-2012, 06:56 AM
  4. Repaint calling Statement Doesn't Work in my code ?
    By nitin_daviet88 in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-24-2010, 04:09 PM
  5. This code doesn't work. Why? thanks.
    By seanzhang in forum New To Java
    Replies: 10
    Last Post: 07-09-2010, 06:53 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •