Reputation: 45
I have the following homework assignment. Our teacher hasn't taught us well, so I need extra help.
Write a Java program that will input five scores of a student in five quizzes. Output the sum and average of five scores. Determine and display the highest and lowest score.
This is how I am stuck now. I only have up to Q3 last time and it only shows the average and the lowest score with no highest score. Kindly help me make this work!
import java.util.Scanner;
import java.io.*;
class Quizzes
{
public static void main(String[] args) {
int Q1, Q2, Q3, Q4, Q5;
double average;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Quizzes: ");
Q1 = keyboard.nextInt();
Q2 = keyboard.nextInt();
Q3 = keyboard.nextInt();
Q4 = keyboard.nextInt();
Q5 = keyboard.nextInt();
average = (Q1 + Q2 + Q3 + Q4 + Q5) / 5;
System.out.println("Average Score is: " + average);
if (Q1 < Q2 && Q1 < Q3) {
System.out.println("The lowest score is: " + Q1);
} else if (Q2 < Q1 && Q2 < Q3) {
System.out.println("The lowest score is: " + Q2);
} else if (Q3 < Q1 && Q2 < Q3) {
System.out.println("The lowest score is: " + Q3);
} else if (Q1 > Q2 && Q1 > Q3) {
System.out.println("The highest score is: " + Q1);
} else if (Q2 > Q1 && Q2 > Q3) {
System.out.println("The highest score is: " + Q2);
} else if (Q3 > Q3 && Q1 > Q2) {
System.out.println("The highest score is: " + Q3);
} else {
System.out.println("The lowest score is: " + Q3);
}
}
}
Upvotes: 0
Views: 3934
Reputation: 3192
In your current solution you have a couple of basic issues such as else if (Q3 > Q3)
.
You also don't check for equality i.e. if (Q2 >= Q3)
.
The major issue with your current technique is that you have a load of if/else statements, a better solution would be to use an Array like below:
public class Quizzes {
public static void main(String[] args) {
double Q1, Q2, Q3, Q4, Q5;
double average = 0;
double max = 0;
double count = 0;
double[] userInput = new double[5];
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Quizzes: ");
System.out.println("");
Q1 = keyboard.nextInt();
userInput[0] = Q1;
Q2 = keyboard.nextInt();
userInput[1] = Q2;
Q3 = keyboard.nextInt();
userInput[2] = Q3;
Q4 = keyboard.nextInt();
userInput[3] = Q4;
Q5 = keyboard.nextInt();
userInput[4] = Q5;
keyboard.close();
for (int i = 0; i < userInput.length; i++) {
count = count + userInput[i];
average = count / 5;
}
System.out.println("Average Score is: " + average);
Arrays.sort(userInput);
System.out.println("The lowest score is: " + userInput[0]);
max = userInput[userInput.length - 1];
System.out.println("The highest score is: " + max);
}
}
EDIT:
Now that you have informed us you cannot use an Array then below code will do the job without modifying your original method too much:
public class Quizzes {
public static void main(String[] args) {
double Q1, Q2, Q3, Q4, Q5;
double average = 0;
double lowest = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Quizzes: ");
System.out.println("");
Q1 = keyboard.nextDouble();
Q2 = keyboard.nextDouble();
Q3 = keyboard.nextDouble();
Q4 = keyboard.nextDouble();
Q5 = keyboard.nextDouble();
keyboard.close();
average = (Q1 + Q2 + Q3 + Q4 + Q5) / 5;
System.out.println("Average Score is: " + average);
if (Q1 <= Q2 && Q1 <= Q3 && Q1 <= Q4 && Q1 <= Q5) {
lowest = Q1;
} else if (Q2 <= Q1 && Q2 <= Q3 && Q2 <= Q4 && Q2 <= Q5) {
lowest = Q2;
} else if (Q3 <= Q1 && Q3 <= Q2 && Q3 <= Q4 && Q3 <= Q5) {
lowest = Q3;
} else if (Q4 <= Q1 && Q4 <= Q2 && Q4 <= Q3 && Q4 <= Q5) {
lowest = Q4;
} else if (Q5 <= Q1 && Q5 <= Q2 && Q5 <= Q3 && Q5 <= Q4) {
lowest = Q5;
}
System.out.println("The lowest score is: " + lowest);
// THEN DO SIMILAR TO GET HIGHEST SCORE
}
}
Upvotes: 5
Reputation: 5048
You can also insert these values into an array, go over this array and look for the min and max values.
Like this:
int[] arr = new int[5];
arr[0] = Q1;
arr[1] = Q2;
arr[2] = Q3;
arr[3] = Q4;
arr[4] = Q5;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i=0; i<arr.length; i++){
min = Math.min(min, arr[i]);
max = Math.min(max, arr[i]);
}
Upvotes: 2
Reputation: 1576
If you're not allowed to use arrays and/or loops, here's one way to do it. You can see why arrays make it more concise; you would not want to do this for 100 questions. I have left out computation of the maximum, since it's trivially similar.
int min = Integer.MAX_VALUE;
if (Q1 < min) {
min = Q1;
}
if (Q2 < min) {
min = Q2;
}
if (Q3 < min) {
min = Q3;
}
if (Q4 < min) {
min = Q4;
}
if (Q5 < min) {
min = Q5;
}
Upvotes: 1