Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java assignment, concerning Arrays and Loops

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java assignment, concerning Arrays and Loops

    I'm incredibly new to writing Java code (using Netbeans, by the way). For my assignment, I have to use arrays to calculate the sum, average, largest, and smallest of any number of test scores. In addition, I have to use a "for loop" to tally up the number of A'S, B'S, C'S, D'S, and F'S (ex. "The number of students with scores of 90-100 (A) is 4", etc. for each letter grade) based on the test scores. All this has to be shown in a message box. I have a message box that will come up to let the user input how many test scores there will be, and what they are. I can calculate the sum, average, largest, and smallest parts of this assignment, but I can't get the latter half of the assignment done right. Instead of "The number of students with scores of 90-100 (A) is (insert calculated number here)" showing up, I get "The number of students with scores of 90-100 (A) is 0", and this happens for each letter grade I'm trying to determine. I'm not sure why the 0 is showing up. Here is my code:


    import javax.swing.JOptionPane;
    public class ArrayProject3 {
     
        public static void main(String[] args) {
     
            String response = JOptionPane.showInputDialog(null, "Number of scores:");
            int number = Integer.parseInt(response);
            int scores[] = new int[number];
            for (int a = 0; a < number; a++) {
                response = JOptionPane.showInputDialog(null, "Enter score " + (a+1));
                scores[a] = Integer.parseInt(response);
            }
            int count = 0;
            int gradeA = 0;
            int gradeB = 0;
            int gradeC = 0;
            int gradeD = 0;
            int gradeF = 0;
            int sum = 0;
            int largest = scores[0];
            int smallest = scores[0];
     
            for (int i = 0; i < scores.length; i++) {
                sum = sum + scores[i];
                if (scores[i] > largest) {
                    largest = scores[i];
                }
                if (scores[i] < smallest) {
                    smallest = scores[i];
                }
                if (scores[i] <= 90 && scores[i] >= 100){
                    gradeA = count++;}
                if (scores[i] <= 80 && scores[i] >= 89){
                    gradeB = count++;}
                if (scores[i] <= 70 && scores[i] >= 79){
                    gradeC = count++;}
                if (scores[i] <= 60 && scores[i] >= 69){
                    gradeD = count++;}
                if (scores[i] <= 60){
                    gradeF = count++;}
                }
     
            JOptionPane.showMessageDialog(null, "The sum is " + sum
                    + "\nThe average is " + (sum / scores.length)
                    + "\nThe largest is " + largest
                    + "\nThe smallest is " + smallest
                    + "\nThe number of students with scores of 90-100 (A) " + gradeA
                    + "\nThe number of students with scores of 80-89 (B) " + gradeB
                    + "\nThe number of students with scores of 70-79 (C) " + gradeC
                    + "\nThe number of students with scores of 60-69 (D) " + gradeD
                    + "\nThe number of students with scores below 60 (F) " + gradeF);
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Java assignment, concerning Arrays and Loops

    First, inspect the logic of those if statements to get the gradeA,B,etc...a value can never be less than/equal to 90 AND greater than/equal to 100. Second, inspect how count is incremented each time through - each time it is looped count will increment, resulting in an incorrect grade count (hint: increment the variable itself).

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java assignment, concerning Arrays and Loops

    Okay, I got the program working! THANK YOU for your help copeg. In my "for loop", I replaced the "&&" with just "&". I also removed the "count" variable completely and incremented the gradeA, gradeB, etc. variables inside the "for loop" with "gradeA = gradeA + 1". Here is the working code:

    import javax.swing.JOptionPane;
    public class ArrayProject3 {
        public static void main(String[] args) {
     
            String response = JOptionPane.showInputDialog(null, "Number of scores:");
            int number = Integer.parseInt(response);
            int scores[] = new int[number];
            for (int a = 0; a < number; a++) {
                response = JOptionPane.showInputDialog(null, "Enter score " + (a+1));
                scores[a] = Integer.parseInt(response);
            }
     
            int gradeA = 0;
            int gradeB = 0;
            int gradeC = 0;
            int gradeD = 0;
            int gradeF = 0;
            int sum = 0;
            int largest = scores[0];
            int smallest = scores[0];
     
            for (int i = 0; i < scores.length; i++) {
                sum = sum + scores[i];
                if (scores[i] > largest) {
                    largest = scores[i];
                }
                if (scores[i] < smallest) {
                    smallest = scores[i];
                }
                if (scores[i] >= 90 & scores[i] <= 100){
                    gradeA = gradeA + 1;}
                if (scores[i] <= 89 & scores[i] >= 80){
                    gradeB = gradeB + 1;}
                if (scores[i] <= 79 & scores[i] >= 70){
                    gradeC = gradeC + 1;}
                if (scores[i] <= 69 & scores[i] >= 60){
                    gradeD = gradeD + 1;}
                if (scores[i] < 60){
                    gradeF = gradeF + 1;}}
     
            JOptionPane.showMessageDialog(null, "The sum is " + sum
                    + "\nThe average is " + (sum / scores.length)
                    + "\nThe largest is " + largest
                    + "\nThe smallest is " + smallest
                    + "\nThe number of students with scores of 90-100 (A) " + gradeA
                    + "\nThe number of students with scores of 80-89 (B) " + gradeB
                    + "\nThe number of students with scores of 70-79 (C) " + gradeC
                    + "\nThe number of students with scores of 60-69 (D) " + gradeD
                    + "\nThe number of students with scores below 60 (F) " + gradeF);
        }
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Java assignment, concerning Arrays and Loops

    I replaced the "&&" with just "&"
    Big difference between the two - while the code may 'work', stick to &&

    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. [SOLVED] A little assignment involving arrays.
    By Melawe in forum What's Wrong With My Code?
    Replies: 39
    Last Post: May 1st, 2011, 11:43 PM
  2. Grades project/arrays, loops
    By rochla16 in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2011, 01:26 AM
  3. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  4. Java Loops help please?
    By Sarah-Perkin in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2009, 02:52 PM
  5. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 04:10 AM