-1

First time poster here. I'm aware of the negative stigma carried with asking for help on homework assignments, however I believe this would be an exception as this is an intro course and the professor stated specifically to use Google to find examples of for loops in Java (of which we have yet to even cover in class). I have absolutely no Java experience and would really appreciate any feedback:

Program asks user how many grades there are.

Program asks user for each grade (for loop needed and should sum grades within loop).

Take sum of all grades, compute average and store in a float variable grade.

Print grade value to console and append a number to a string such as "Grade Average is: " + grade

Example should read as:

Enter number of grades: 2 Enter grade: 90 Enter grade: 81 Grade Average is: 85.5

My code so far (not much here):

// This program computes the letter grades for number of grades given by user

import java.util.*;

public class GradeAverage
{
    public static void main(String[] args)
    {
        int count;
        float sum = 0;
        float grade;
        Scanner scan = new Scanner(System.in);
    }
}

Edit:

// This program computes the letter grades for number of grades given by user

import java.util.*;

public class GradeAverage
{
    public static void main(String[] args)
    {
        int count;
        float sum = 0;
        float grade;
        Scanner scan = new Scanner(System.in);

        System.out.print("Enter number of grades: ");
        count = scan.nextInt();

        for (int i = 0; i < count; ++i)
            System.out.print("Enter grade " + (i + 1) + ": ");
            grade = scan.nextFloat();
            sum += grade;

        System.out.println("The average of the grades is: " + sum/count);
    }
} 

This is what I have now, however a test displays incorrect results (example):

Enter number of grades: 2 Enter grade 1: Enter grade 2: 50 50 The average of the grades is: 25.0

Each grade needs to be entered on separate lines so the averaging is skewed as a result.

BJ Myers
  • 6,073
  • 6
  • 33
  • 45
Tekt
  • 1
  • 1
  • 1
  • 4
  • 1
    Okay. So how would you do this *on paper*? Note that the question says "Take sum of all grades" and then later, get the average. After the loop two things should be known: the `sum` of grades (where each grade is entered per loop) and the `count` (entered in the first prompt) of grades entered. I see neither a first-prompt (for the `count`) nor a loop (using the `count`) and a prompt asking for a grade (to add to `sum`). – user2864740 Sep 12 '14 at 22:59
  • 1
    Just a quick side note: homework questions are technically fine, so long as they follow the other question guidelines (research effort and all that). However I do think this question is overly broad, as you're not asking about a specific question or issue, but more "general feedback", which isn't great. It becomes hard to give you an answer / help you without giving away too much and robbing you of a good learning opportunity. – Paul Richter Sep 12 '14 at 23:17
  • You are missing the curly brackets around the 3 lines in your loop. That is why only the print lines are looping: without these brackets java only looped the following line. Indentation is ignored by the java compiler. – nmore Sep 13 '14 at 00:31
  • @nmore Where do these need to go in relation to my revised code above? Do I need to remove the breaks? – Tekt Sep 13 '14 at 00:55
  • @nmore I just got it. Thank you. – Tekt Sep 13 '14 at 00:57

4 Answers4

0

Break the big task into smaller tasks , like @user2864740 said , write the algorithm (not code) on a paper then start translating that to code

u reached the part where u created a scanner to read input , now read the input and ....figure out the rest .

To learn how to read user input read this.

To learn how to make an integer out of Strings ur scanning read this

the rest is basic math really , read your textbook , and good luck ;)

edit : at least come out with some algorithm then maybe we'll help with the code

Community
  • 1
  • 1
vlatkozelka
  • 769
  • 10
  • 25
  • I will try this. There is no textbook for this class. Also, the code posted was provided by the professor to preface the portion we need to input. – Tekt Sep 12 '14 at 23:09
0

I won't solve the homework for you, I will help you however:

How to use a for loop:

for (int i = #startValue#; #booleanCondition#; #runTheFollowingCodeAtEachIteration#)
{
    //code
}

ex:

for(int i = 0; i<10; i++)
{
    System.out.println(i);
}

will display:

0  
1  
2  
3  
4  
5  
6  
7  
8  
9  

Your homework:

Program asks how many grades there are:
Scan a value called NumberOfGrades (called count in your code) inputted by the user.

Program asks user for each grade + sums the grades:
Use a for loop, with a starting value of i, and a upper limit of NumberOfGrades. Scan each grade and add it to a value called GradeSum, which initially should be 0 before entering the for loop.

Print value to console... :
Divide GradeSum by NumberofGrades, and display it how you would like it to be displayed.

Tips:
-Use System.out.print("\nEnter grade: "); in your for loop before each scan.

ThaBomb
  • 642
  • 6
  • 11
0

To avoid directly answering your homework question (which both won't help you get it and is probably not allowed), let's start with "what is a for loop?"

A for loop is a fancy loop that does the following things for you:

  1. Initializes one (or more) variables to initial values the first time the loop statement is executed
  2. Each iteration, checks a boolean condition to determine if it should loop again. If the expression evaluates to true, iterate again. Otherwise, break the loop and continue with the code following the loop.
  3. A statement that is run each time the loop finishes iterating.

For example, the following loop would print the numbers 1 .. 10.

for(int i = 1; i <= 10; i++){
    System.out.println(i);
}
  • The first part of the loop statement int i = 1 is the initialization block. i is initialized to an int with value 1 when the for loop is executed for the first time.
  • The second part of the loop statement i <= 10 is the boolean condition to check to determine if another iteration is required. In this case, i <= 10 evaluates to true if i is less than or equal to 10, and false once i hits 11 (or any larger number).
  • Finally, the third part i++ is the statement run when the for loop finishes an iteration. i++ adds 1 to the current value of i, thus i will increase in value by 1 each iteration.
Mshnik
  • 6,944
  • 1
  • 21
  • 37
0
import java.util.Scanner;     
public static void main(String[] args) {
            int count = 0;
            float sum = 0;
            float grade = 0;
            Scanner scan = new Scanner(System.in);

            System.out.print("Enter number of grades: ");
            count = scan.nextInt();

            for (int i = 0; i < count; i++) {
                System.out.print("Enter grade no " + (i + 1) + " : ");
                grade = scan.nextFloat();
                sum += grade;
            }
            System.out.println("Sum = " + sum);
            System.out.println("Average = " + sum / (float) count);
        }
Shane
  • 128
  • 1
  • 2
  • 15