Hi guys basically im new to Java started in oct, im doing ComScience. I need help please with the following would be greatly appreciated.
NOTE: I have not really laid the code out yet so to speak i wanted to get the program to work first before structuring/comments etc . UULIB is name of the University librays.
OBJECTIVE: Find the NAME of students who have below AVERAGE mark.
This is the code i used below to try and find it but it skips the first [0] array number and gives me one name only.
import uulib.*;
public class Q3
{
public static void main(String[] args)
{
int class_number = Console.getInt("Enter how many students between 1-10");
while (class_number < 1 || class_number > 10)
class_number = Console.getInt("Enter how many students between 1-10");
System.out.println("");
String[] name = new String[class_number];
int[] score = new int[name.length];
for (int i=0; i<class_number; i=i+1)
{
name[i] = Console.getString("Enter Name");
score[i] = Console.getInt("Enter score");
if ( score[i] < average(score))
System.out.print(name[i] + " ");
}
System.out.println(" ");
System.out.println("Average mark = " + Num.format(average(score), 1 ));
System.out.println("Lowset mark = " +lowset(score) );
System.out.println("Highest mark = " +highestValue(score) );
System.out.println("Name of students with highest mark = " );
}
public static double average(int[] nums)
{
double total = 0;
for (int i=0; i<nums.length; i=i+1)
{
total = total + nums[i];
}
return total / nums.length;
}
public static int lowset(int[] nums)
{
int minimum = nums[0]; //sets the first to be the smallest
for (int i = 0; i < nums.length; i++) //goes through your array
{
if (nums[i] < minimum) //checks and replaces if necessary
{
minimum = nums[i];
}
}
return minimum;
}
private static int highestValue(int[] numbers)
{
int highest = numbers[0];
for (int i = 0; i < numbers.length; i++)
{
if (numbers[i] > highest)
{
highest = numbers[i];
}
}
return highest;
}
}
You should split your for-loop into two for-loops as you can’t compute the average before having collected all scores: