0

Hi guys,

This is my first post and basically i require some help editing my current code to help display number of students with a mark of 40 or above specifically. And also i am having difficulty generating a average i thought i done it but it remained constant a 4.0 with every test. This work is on going so i will be constantly updating the page each time i run into more troubles. Thanks guys below is a copy of my code and how far i got...

package histogram1;

import java.util.Scanner;

public class Histogram1

public static void main(String[] args) 

    Scanner MK = new Scanner(System.in);

    int[] ranges  =  {0,29,39,69,100 };

    int[] inRange = new int[ranges.length - 1];

    int mark;

    int largest = 0;

    int smallest = 0;

    do 

        System.out.println("Enter Mark:");

        mark = MK.nextInt();       

        for (int j=1 ; j<ranges.length ; j++)

           if (ranges[j-1] <= mark && mark <= ranges[j]) 

               inRange[j-1]++;

               break;

           if (mark < smallest) 

                smallest = mark;

            else if (mark > largest) 

                largest = mark;

     while (mark >= 0);

            System.out.println("Among all your numbers, " + smallest + " is the smallest " + "and " + largest + " is the largest number.");

for (int j = 0; j < k; j++)

                    mark += ranges.length;

                double avg = (double) (mark) / k;

                System.out.println("average is " + avg);

    int average;

    String s = "The number of students that have scored between %d and %d is : ";

    int k = 0;

    for (int i=0 ; i<ranges.length - 1 ; i++) 

      System.out.print(String.format(s,ranges[i] + k,ranges[i + 1]));   

        for (int r = 0; r<inRange[i] ; r++)

            System.out.print("*");

        System.out.println();

        k = 1;

    MK.close();
3
Contributors
2
Replies
19
Views
4 Weeks
Discussion Span
Last Post by JamesCherrill
0

In the first loop, you have a block so, I think it must be :

do {
    //
}while( //condition);

In the second loop, I don't get the first line :

mark += ranges.length;

I think you must rethink a good part of the program.

0

Yes - as above.
In fact all your loops and if/else blocks need brackets.
The code inside your loop line 15 will be executed for the "end of input" value (<=0), which will corrupt your results.
Also smallest is initialised to zero, so if (mark < smallest) will never be true.

Edited by JamesCherrill

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.