How do I get "Enter a grade : " before every grade input and how do I change it so -1 os the end of the input? java. import java.util.Scanner; // Grade class public class Grades { // Function to read input from user public static int readGrades() { int i=0; Scanner input = new Scanner(System.in); i=input.nextInt(); return i; } // Function to calculate the sum of grades public static int sum(int[] arr) { int grade_sum=0; for(int i=0;iarr[i]) { min=arr[i]; } } return min; } // Function to calculate the minimum value of grades public static int minValue(int[] arr, int firstIndex, int lastIndex) { int min=arr[0]; for(int i=firstIndex;i<=lastIndex;i++) { if(min>arr[i]) { min=arr[i]; } } return min; } // Function to calculate the index of minimum value of grades public static int indexOfFirstMinValue(int[] arr) { int min=0; for(int i=0;iaverage(arr)) { count++; } } return count; } // Rotate the array public static void rotateElements(int[] arr) { int i, temp; temp = arr[0]; for (i = 0; i < arr.length - 1; i++) { arr[i] = arr[i + 1];} arr[arr.length-1] = temp; /*printing the rotated array*/ System.out.print("\nRotated array is: \n"); for (int k = 0; k < arr.length; k++) { System.out.print(arr[k]+" "); } } // Rotate the array for a number of times public static void rotateElements(int[] arr, int rotationCount) { for (int i = 0; i < rotationCount; i++) rotateElements(arr); /*printing the rotated array*/ System.out.print("\nRotated array is: \n"); for (int k = 0; k < arr.length; k++) { System.out.print(arr[k]+" "); } } // Function to reverse the elements of the array public static void reverseArray(int[] arr) { int n= arr.length; int[] b = new int[n]; int j = n; for (int i = 0; i < n; i++) { b[j - 1] = arr[i]; j = j - 1; } /*printing the reversed array*/ System.out.print("\nReversed array is: \n"); for (int k = 0; k < n; k++) System.out.print(b[k]+" "); } // Driver code public static void main(String[] args) { int grade[]=new int[5]; int i=0; Scanner input = new Scanner(System.in); System.out.println("Enter a grade : "); while(i<5) { grade[i]=readGrades(); i++; } System.out.println("\nThe maximum grades is: "+maxValue(grade)); System.out.println("\nThe minimum grades is: "+minValue(grade)); } }
How do I get "Enter a grade : " before every grade input and how do I change it so -1 os the end of the input? java.
import java.util.Scanner;
// Grade class
public class Grades {
// Function to read input from user
public static int readGrades() {
int i=0;
Scanner input = new Scanner(System.in);
i=input.nextInt();
return i;
}
// Function to calculate the sum of grades
public static int sum(int[] arr) {
int grade_sum=0;
for(int i=0;i<arr.length;i++)
grade_sum=grade_sum+arr[i];
return grade_sum;
}
// Function to calculate the sum of grades
public static int sum(int[] arr, int firstIndex, int lastIndex) {
int grade_sum=0;
for(int i=firstIndex;i<=lastIndex;i++)
grade_sum=grade_sum+arr[i];
return grade_sum;
}
// Function to calculate the average of grades
public static double average(int[] arr) {
double sum=sum(arr);
return ((float)sum/(float)(arr.length));
}
// Function to calculate the maximum value of grades
public static int maxValue(int[] arr) {
int max=0;
for(int i=0;i<arr.length;i++) {
if(max<arr[i]) {
max=arr[i];
}
}
return max;
}
// Function to calculate the maximum value of grades
public static int maxValue(int[] arr, int firstIndex, int lastIndex) {
int max=0;
for(int i=firstIndex;i<=lastIndex;i++) {
if(max<arr[i]) {
max=arr[i];
}
}
return max;
}
// Function to calculate the index of maximum value of grades
public static int indexOfFirstMaxValue(int[] arr) {
int max=0;
for(int i=0;i<arr.length;i++) {
if(maxValue(arr)==arr[i]) {
max=i;
}
}
return max;
}
// Function to calculate the minimum value of grades
public static int minValue(int[] arr) {
int min=arr[0];
for(int i=1;i<arr.length;i++) {
if(min>arr[i]) {
min=arr[i];
}
}
return min;
}
// Function to calculate the minimum value of grades
public static int minValue(int[] arr, int firstIndex, int lastIndex) {
int min=arr[0];
for(int i=firstIndex;i<=lastIndex;i++) {
if(min>arr[i]) {
min=arr[i];
}
}
return min;
}
// Function to calculate the index of minimum value of grades
public static int indexOfFirstMinValue(int[] arr) {
int min=0;
for(int i=0;i<arr.length;i++) {
if(minValue(arr)==arr[i]) {
min=i;
}
}
return min;
}
// Function to calculate the grade count less than the average grades
public static int numberOfBelowAverageElements(int[] arr) {
int count=0;
for(int i=0;i<arr.length;i++) {
if(arr[i]<average(arr)) {
count++;
}
}
return count;
}
// Function to calculate the grade count above the average grades
public static int numberOfAboveAverageElements(int[] arr) {
int count=0;
for(int i=0;i<arr.length;i++) {
if(arr[i]>average(arr)) {
count++;
}
}
return count;
}
// Rotate the array
public static void rotateElements(int[] arr) {
int i, temp;
temp = arr[0];
for (i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];}
arr[arr.length-1] = temp;
/*printing the rotated array*/
System.out.print("\nRotated array is: \n");
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[k]+" ");
}
}
// Rotate the array for a number of times
public static void rotateElements(int[] arr, int rotationCount) {
for (int i = 0; i < rotationCount; i++)
rotateElements(arr);
/*printing the rotated array*/
System.out.print("\nRotated array is: \n");
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[k]+" ");
}
}
// Function to reverse the elements of the array
public static void reverseArray(int[] arr) {
int n= arr.length;
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = arr[i];
j = j - 1;
}
/*printing the reversed array*/
System.out.print("\nReversed array is: \n");
for (int k = 0; k < n; k++)
System.out.print(b[k]+" ");
}
// Driver code
public static void main(String[] args) {
int grade[]=new int[5];
int i=0;
Scanner input = new Scanner(System.in);
System.out.println("Enter a grade : ");
while(i<5) {
grade[i]=readGrades();
i++;
}
System.out.println("\nThe maximum grades is: "+maxValue(grade));
System.out.println("\nThe minimum grades is: "+minValue(grade));
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images