-1

I am doing a class in Computer Programming and right now we're using Arrays. My objective is to do the following:

~ Prompt users for how many math tests they have written this year. ~ Asks the users their score on each of these math tests. ~ Place each score in an array. ~ Sort the array and give their math scores from least to greatest. ~ Calculate an average of the math scores.

My problem occurs when it just only keeps the scores at 0 and later wont recognize the testGrade int variable so it won't average them.

Here is my full code as of right now:

public static void main(String... args) throws Exception
{
    for (int i = 0;i < testResults.length; i++)
    {
        System.out.print("Thank you, now please enter your grade on each test:     ");
        int testGrades = k.nextInt();
    }
    System.out.print("The original sequence is: \n   ");
    for (int i = 0;i < testResults.length; i++)
    {
        System.out.print(testResults [i] + ", ");
    }
    System.out.println();
    SortEm(testResults);
    System.out.print("The new sequence is : \n   ");
    for (int i=0; i < testResults.length; i++)
    {
        System.out.print (testResults[i] + ", ");
    }
    System.out.println();
    for (int i = 0;i < testResults.length; i++)
    {
        System.out.println("The average of all your tests is " + (testGrades / testNum));
    }
}
private static void SortEm (int [] ar)
{
    int temp;
    for (int i = ar.length - 1; i > 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (ar[j] > ar[j + 1])
            {
                temp = ar[j];
                ar[j] = ar[j + 1];
                ar[j+1] = temp;
            }
        }
    }
}

I would really appreciate some insight on what's going on and how to fix it. Thank you all in advance :)

My Errors after answer: 6 errors found: File: C:\Users\herhstudent\Desktop\gg.java [line: 1] Error: Syntax error on tokens, delete these tokens File: C:\Users\herhstudent\Desktop\gg.java [line: 3] Error: Syntax error on token "void", @ expected File: C:\Users\herhstudent\Desktop\gg.java [line: 3] Error: Syntax error on token "...", . expected File: C:\Users\herhstudent\Desktop\gg.java [line: 3] Error: Syntax error on token "throws", interface expected File: C:\Users\herhstudent\Desktop\gg.java [line: 6] Error: Syntax error on token ";", { expected after this token File: C:\Users\herhstudent\Desktop\gg.java [line: 44] Error: Syntax error, insert "}" to complete InterfaceBody

  • Possible duplicate of [how many times the array contains the value?](https://stackoverflow.com/questions/49958163/how-many-times-the-array-contains-the-value), do `testResults[i] = testGrades;` – Pavneet_Singh Apr 24 '18 at 16:25
  • `testResults`, `testGrades`, `testNum` and `k` are all undefined in the code sample you've provided – Michael Apr 24 '18 at 16:26

3 Answers3

1

Your program isn't recognizing your variables because you declare them inside loops. Variables that are declared inside of a loop do not exist outside of the loop. Try this:

int tesGrades = 0;
for (int i = 0;i < testResults.length; i++)
{
    System.out.print("Thank you, now please enter your grade on each test:     ");
    testGrades = k.nextInt();
}

Also the way you do this loop, testGrades keeps being overridden, and you won't have any input except the last. Try:

testResults[i] = k.nextInt();

Instead of:

testGrades = k.nextInt();

This will populate the testResults array with all the inputted test scores.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
1

Multiple problems here. The first is that testGrades is declared in the for-loop:

for (int i = 0;i < testResults.length; i++)
{
    System.out.print("Thank you, now please enter your grade on each test:     ");
    int testGrades = k.nextInt();
}

This means is has a narrower scope. It is not available later. You can move it to be declared before the for-loop but that still doesn't really make sense. This variable should represent the total score and we should be progressively incrementing it, not setting it every time. I've also renamed it so it's more obvious what it's doing:

int totalScore = 0;
for (int i = 0;i < testResults.length; i++)
{
    System.out.print("Thank you, now please enter your grade on each test:     ");
    totalScore += k.nextInt();
}

Now we need to address that you're never actually writing any values to the array. This loop now becomes:

for (int i = 0;i < testResults.length; i++)
{
    System.out.print("Thank you, now please enter your grade on each test:     ");
    testResults[i] = k.nextInt();
    totalScore += testResults[i];
}

Finally, at the end we don't need to loop to print the average. We can just do it once. We don't need the variable testNum because we can just use testResults.length as you do previously.

System.out.println("The average of all your tests is " + (totalScore / testResults.length));

The final problem you'll run into is integer division. I'll leave you to solve that on your own.

Final code:

private static int[] testResults = new int[5];

public static void main(String... args) throws Exception
{
    Scanner k = new Scanner(System.in);
    int testGrades = 0;

    for (int i = 0;i < testResults.length; i++)
    {
        System.out.print("Thank you, now please enter your grade on each test:     ");
        testResults[i] = k.nextInt();
        testGrades += testResults[i];
    }
    System.out.print("The original sequence is: \n   ");
    for (int i = 0;i < testResults.length; i++)
    {
        System.out.print(testResults [i] + ", ");
    }
    System.out.println();
    SortEm(testResults);
    System.out.print("The new sequence is : \n   ");
    for (int i=0; i < testResults.length; i++)
    {
        System.out.print (testResults[i] + ", ");
    }
    System.out.println();
    System.out.println("The average of all your tests is " + (testGrades / testResults.length));
}
private static void SortEm (int [] ar)
{
    int temp;
    for (int i = ar.length - 1; i > 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (ar[j] > ar[j + 1])
            {
                temp = ar[j];
                ar[j] = ar[j + 1];
                ar[j+1] = temp;
            }
        }
    }
}
Michael
  • 41,989
  • 11
  • 82
  • 128
  • also I don't understand why you changed - public static void main(String[] args) - to - public static void main(String... args) throws Exception - – MacKenzie Cutler Apr 24 '18 at 16:50
  • @MacKenzieCutler I have run it from my IDE and it works correctly. Post the errors if you like. The signature change to `main` is not relevant. IntelliJ did it automatically. – Michael Apr 24 '18 at 16:51
  • As part of my class I need to use the program Dr. Java and these are my errors: I added it to the question because it exceeds the character limit – MacKenzie Cutler Apr 24 '18 at 16:52
0

change first loop to this:

for (int i = 0;i < testResults.length; i++)
    {
        System.out.print("Thank you, now please enter your grade on each test:     ");
        testResults[i] = k.nextInt(); // <-- 
    }
ygngy
  • 3,630
  • 2
  • 18
  • 29