My project is about finding the average, minimum and maximum of students' grades and their standing, but it keeps showing me an error on the output.
Here are the names and the grades:
Students:
Alia Nahid Eiman Suad Lamia Salma Mai Wedad Haya Sanaa
Quiz:
10 20 50 70 80 50 30 90 60 40
Midterm:
30 80 100 40 80 70 70 80 50 30
Final Exam:
40 80 70 100 90 60 70 50 40 80
And here is the Java code:
package java_final_project;
import java.util.*;
public class Java_Final_Project {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// Declare inputs:
String letter1, letter2, letter3;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
// Declare the four array structures
int[] quiz = new int[10];
int[] midterm = new int[10];
int[] finalexam = new int[10];
String[] students = new String[10];
// Input elements to the arrays
int counter, sum1 = 0;
int sum2 = 0;
int sum3 = 0;
double ave1, ave2, ave3;
int max1, max2, max3;
int min1, min2, min3;
for (counter = 0; counter < 10; counter++) {
System.out.println ("Enter student names and grades of quiz, midterm "
+ "and final with space between them");
students[counter] = console.next();
quiz[counter] = console.nextInt();
sum1 = sum1 + quiz[counter];
midterm[counter]= console.nextInt();
sum2 = sum2 + midterm[counter];
finalexam[counter]= console.nextInt();
sum3 = sum3 + finalexam[counter];
}
ave1 = sum1 / 10;
ave2 = sum2 / 10;
ave3 = sum3 / 10;
// min and max values
max1 = Math.max(quiz[counter], max);
max2 = Math.max(midterm[counter], max);
max3 = Math.max(finalexam[counter], max);
min1 = Math.min(quiz[counter], min);
min2 = Math.min(midterm[counter], min);
min3 = Math.min(finalexam[counter], min);
// if statement for the standing
// ave1
if (ave1 >= 90 && ave1 <= 100) {
letter1 = "E";
} else if (ave1 >= 70 && ave1 <= 80) {
letter1 = "G";
} else if (ave1 >= 50 && ave1 <= 60) {
letter1 = "S";
} else {
letter1 = "P";
}
// ave2
if (ave2 >= 90 && ave2 <= 100) {
letter2 = "E";
} else if (ave2 >= 70 && ave2 <= 80) {
letter2 = "G";
} else if (ave2 >= 50 && ave2 <= 60) {
letter2 = "S";
} else {
letter2 = "P";
}
//ave3
if (ave3 >= 90 && ave3 <= 100) {
letter3 = "E";
} else if (ave3 >= 70 && ave3 <= 80) {
letter3 = "G";
} else if (ave3 >= 50 && ave3 <= 60) {
letter3 = "S";
} else {
letter3 = "P";
}
// Display the elements of the four arrays
System.out.println("Here is the elements of the four arrays");
System.out.println("students" + "\t" + "quiz" + "\t" + "midterm" + "\t" + "finalexam");
for (counter = 0; counter < 10; counter++) {
System.out.println(students[counter] + "\t" + quiz[counter] + "\t" + midterm[counter] + "\t" + finalexam[counter]);
}
System.out.println("Summary Report:");
System.out.println();
System.out.println("students" + "\t" + "\t" + "Quiz" + "\t" + "\t" + "Midterm" + "\t" + "\t" + "FinalExam");
System.out.println();
System.out.println("Average" + "\t" + "\t" + ave1 + "\t" + "\t" + ave2 + "\t" + "\t" + ave3);
System.out.println();
System.out.println("Max" + "\t" + "\t" + max1 + "\t" + "\t" + max2 + "\t" + "\t" + max3);
System.out.println();
System.out.println("Min" + "\t" + "\t" + min1 + "\t" + "\t" + min2 + "\t" + "\t" + min3);
System.out.println();
System.out.println("Standing" + "\t" + "\t" + letter1 + "\t" + "\t" + letter2 + "\t" + "\t" + letter3);
}
}
The six lines where you set min1, min2, min3, max1, max2, max3 need to be up, inside the for loop where you gather the input; because you want to check after each input whether it's bigger or smaller than the current maximum and minimum. Where you have those lines at the moment, they can't possible work, because counter is no longer pointing to an entry in the array.
I'm not sure if you are allowed to use streams in your assignment. If you are then there is a much easier way of getting min, max, average in Java 8:
int[] scores = {1, 3, 6, 8, 10, 11, 2};
int max = Arrays.stream(scores).max();
int min = Arrays.stream(scores).min();
int avg = Arrays.stream(scores).average();
You don't really need to use loops often now that Java has streams.
I've figured out the codes and corrected some mistakes. Here's the updated java code and it worked :D! and I thank you for your comments and help :)
package java_final_project;
import java.util.*;
public class Java_Final_Project
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
// Declare inputs:
String letter1, letter2, letter3;
double ave1, ave2, ave3;
// Declare the four array structures
int[] quiz = new int[10];
int[] midterm = new int[10];
int[] finalexam = new int[10];
String [] students = new String[10];
int [] scores = new int[100];
// Input elements to the arrays
int counter, sum1=0;
int sum2=0;
int sum3=0;
for ( counter = 0; counter < 10; counter++)
{
System.out.println ("Enter student names and grades of quiz, midterm "
+ "and final with space between them");
students[counter] = console.next();
quiz[counter]= console.nextInt();
sum1 = sum1 + quiz[counter];
midterm[counter]= console.nextInt();
sum2 = sum2 + midterm[counter];
finalexam[counter]= console.nextInt();
sum3 = sum3 + finalexam[counter];
}
// max and min values for the quiz
int minGrade = quiz[0];
int maxGrade = quiz[0];
for (int i = 0; i < 10; i++)
{
if (minGrade > quiz[i])
minGrade = quiz[i];
if (maxGrade < quiz[i])
maxGrade = quiz[i];
}
// max and min values for the midterm
int minGrade1 = midterm[0];
int maxGrade1 = midterm[0];
for (int i = 0; i < 10; i++)
{ if (minGrade1 > midterm[i])
minGrade1 = midterm[i];
if (maxGrade1 < midterm[i])
maxGrade1 = midterm[i];}
// max and min values for the final
int minGrade2 = finalexam[0];
int maxGrade2 = finalexam[0];
for (int i = 0; i < 10; i++)
{if (minGrade2 > finalexam[i])
minGrade2 = finalexam[i];
if (maxGrade2 < finalexam[i])
maxGrade2 = finalexam[i];}
//calculate the average of quiz, midterm, final
ave1 = sum1/10;
ave2 = sum2/10;
ave3 = sum3/10;
// if statement for the standing
// ave1
if (ave1 > 90 && ave1 > 100)
{letter1 = "E";}
else if (ave1 > 70 && ave1 > 80)
{letter1 = "G" ;}
else if (ave1 > 50 && ave1 > 60)
{letter1 = "S";}
else
{letter1 = "P";}
// ave2
if (ave2 > 90 && ave2 > 100)
{letter2 = "E";}
else if (ave2 > 70 && ave2 > 80)
{letter2 = "G" ;}
else if (ave2 > 50 && ave2 > 60)
{letter2 = "S";}
else
{letter2 = "P";}
//ave3
if (ave3 > 90 && ave3 > 100)
{letter3 = "E";}
else if (ave3 > 70 && ave3 > 80)
{letter3 = "G" ;}
else if (ave3 > 50 && ave3 > 60)
{letter3 = "S";}
else
{letter3 = "P";}
// Display the elements of the four arrays
System.out.println (" Here is the elements of the four arrays");
System.out.println ("students"+"\t"+"quiz"+"\t"+"\t"+"midterm"+"\t"+"\t"+"finalexam");
for ( counter = 0; counter < 10; counter++)
{
System.out.println (students[counter]+"\t"+"\t"+quiz[counter]+"\t"+"\t"+midterm[counter]
+"\t"+"\t"+finalexam[counter]);
}
System.out.println (" ");
System.out.println ("Summary Report:");
System.out.println ("\t"+"\t"+"Quiz"+"\t"+"\t"+"Midterm"+"\t"+"\t"+"FinalExam");
System.out.println ("Average"+"\t"+"\t"+ave1+"\t"+"\t"+ave2+"\t"+"\t"+ave3);
System.out.println ("max"+ "\t"+"\t"+maxGrade+ "\t"+"\t"+ maxGrade1+ "\t"+"\t"+ maxGrade2);
System.out.println ("min"+ "\t"+"\t"+minGrade+ "\t"+"\t"+ minGrade1+ "\t"+"\t"+ minGrade2);
System.out.println ();
System.out.println ("Standing"+ "\t"+ letter1+ "\t"+ "\t"+ letter2+ "\t"+"\t"+ letter3);
}
}
Related
I am making a dice game, the rules are 2 dice are thrown randomly, if the sum of both dice are 7 or 11 in the first try, the user wins. if the sum is 2,3 or 12 in the first try, the user loses. if the sum is 4,5,6,8,9,10 then that sum is set as an establish point and continues to throw the dice till the sum matches the established point and the user wins, however if the establish point is 7 the user loses.
the game plays 3 times and records how many times I have won or lost. however, I am not able to get that part finished, help would be appreciated, my code is bellow.
package roll;
import java.util.Random;
public class RandomSumGame {
boolean start;
int d1;
int d2;
int sum = d1 + d2;
int valuepoint;
String plus = "+";
public void play(int d1, int d2) {
int win = 0;
int loss = 0;
sum = d1 + d2;
for (;;) {
if (sum == 2 || sum == 3 || sum == 12) {
start = false;
loss++;
System.out.println(" = " + sum + ";you lose");
break;
} else if (sum == 7 || sum == 11) {
start = true;
win++;
System.out.println(" = " + sum + ";you win ");
break;
}
valuepoint = sum;
if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
|| valuepoint == 10) {
System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
break;
}
}
for (;;) {
if (sum == 7 || sum == 11) {
break;
} else {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
int f1 = d1;
System.out.print("\t" + "-you rolled again " + d1 + " " + plus);
for (int x = 0; x < 1; x++) {
d2 = 1 + rand.nextInt(6);
int f2 = d2;
int loda = f1 + f2;
System.out.print(" " + d2 + " = " + loda + "\n");
}
}
sum = d1 + d2;
if (sum == valuepoint) {
start = true;
win++;
System.out.print("\t" + "you win!" + "\n");
break;
} else if (sum == 7) {
loss++;
start = false;
System.out.print("\t" + "you lose " + "\n");
break;
}
}
}
System.out.print("wins: " + win + "\n");
System.out.print("Losses:" + loss);
}
public void play() {
for (int x = 0; x < 3; x++) {
rolldice();
play(d1, d2);
System.out.print("\n");
}
}
public void rolldice() {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
System.out.print("You rolled " + d1 + " " + plus);
}
for (int i = 0; i < 1; i++) {
d2 = 1 + rand.nextInt(6);
System.out.print(" " + d2 + " ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RandomSumGame Teet = new RandomSumGame();
RandomSumGame d1counter = new RandomSumGame();
RandomSumGame d2counter = new RandomSumGame();
Teet.play();
}
}
What part are you not able to finish? Seems like the game is running 3 times? Is it the total number of wins and loses? To fix that, you could just move "int win" and "int lose" outside the play method to make them global variables.
EDIT:
If you don't want to use global variables, you could make a method that calls itself (recursive method)
import java.util.Random;
public class RandomSumGame {
boolean start;
int d1;
int d2;
int sum = d1 + d2;
int valuepoint;
String plus = "+";
public void play(int d1, int d2, int win, int loss) {
sum = d1 + d2;
for (;;) {
if (sum == 2 || sum == 3 || sum == 12) {
start = false;
loss++;
System.out.println(" = " + sum + ";you lose");
break;
} else if (sum == 7 || sum == 11) {
start = true;
win++;
System.out.println(" = " + sum + ";you win ");
break;
}
valuepoint = sum;
if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
|| valuepoint == 10) {
System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
break;
}
}
for (;;) {
if (sum == 7 || sum == 11) {
break;
} else {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
int f1 = d1;
System.out.print("\t" + "-you rolled again " + d1 + " " + plus);
for (int x = 0; x < 1; x++) {
d2 = 1 + rand.nextInt(6);
int f2 = d2;
int loda = f1 + f2;
System.out.print(" " + d2 + " = " + loda + "\n");
}
}
sum = d1 + d2;
if (sum == valuepoint) {
start = true;
win++;
System.out.print("\t" + "you win!" + "\n");
break;
} else if (sum == 7) {
loss++;
start = false;
System.out.print("\t" + "you lose " + "\n");
break;
}
}
}
System.out.print("wins: " + win + "\n");
System.out.print("Losses:" + loss);
if (win < 2 && loss < 2) {
System.out.print("\n");
//Recursive
play(win, loss);
}
}
public void play(int win, int loss) {
rolldice();
play(d1, d2, win, loss);
}
public void rolldice() {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
System.out.print("You rolled " + d1 + " " + plus);
}
for (int i = 0; i < 1; i++) {
d2 = 1 + rand.nextInt(6);
System.out.print(" " + d2 + " ");
}
}
public static void main(String[] args) {
RandomSumGame test = new RandomSumGame();
test.play(0,0);
}
}
Im trying to get my test score program to work. Whenever I type in the pathfile to read from the text file the program would read 4 out of the 5 numbers that are there.Also, no matter what number there are it is always going to display my minimum as 0 when it is not true. Any help is truly appreciated!
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Exam{
public static void main(String[] args) throws IOException {
System.out.println("Welcome!" + "\n");
//File location
System.out.println("Where is the data file:");
Scanner userInput = new Scanner(System.in);
String userFile = userInput.nextLine();
int i = 0;
int scores[] = readScores(userFile);
System.out.println("Minimum score: " + scores[0]);
System.out.println("Maximum score: " + scores[(scores.length - 1)]);
//Average Calculation
double gradesTotal = 0;
for (i=0; i<scores.length; ++i){
gradesTotal = gradesTotal + scores[i];
}
double mean = gradesTotal/scores.length;
System.out.println("Average score: " + mean);
//Mean Calculation
double median;
if (scores.length % 2 == 0)
median = ((scores[(scores.length/2) - 1]) + scores[(scores.length/2)]) / 2;
else
median = scores[(scores.length/2)];
System.out.println("Median score: " + median + "\n");
//Number of Grades
int gradeA = 0;
int gradeB = 0;
int gradeC = 0;
int gradeD = 0;
int gradeF = 0;
for (i=0; i<scores.length; i++)
{
if (scores[i] >= 90 && scores[i] <=100){
gradeA++;
}
else if (scores[i] <= 89 && scores[i] >=80){
gradeB++;
}
else if (scores[i] <= 79 && scores[i] >=70){
gradeC++;
}
else if (scores[i] <= 69 && scores[i] >=60){
gradeD++;
}
else if (scores[i] <= 59 && scores[i] >=1){
gradeF++;
}
}
System.out.println("Scores by letter grade: ");
System.out.println("A: " + gradeA);
System.out.println("B: " + gradeB);
System.out.println("C: " + gradeC);
System.out.println("D: " + gradeD);
System.out.println("F: " + gradeF);
}
//Reads the data from the submitted file
private static int[] readScores(String userFile) throws FileNotFoundException
{
File inputFile = new File(userFile);
Scanner stats = new Scanner(inputFile);
try {
int scores[] = new int[stats.nextInt()];
int i = 0;
while (stats.hasNext()){
scores[i] = stats.nextInt();
i++;
}
System.out.println("\n" + "There are " + (i) + " scores" + "\n");
Arrays.sort(scores);
return scores;
}
finally {
stats.close();
}
}
}
Text file:
72
31
13
39
74
Program output:
There are 4 scores
Minimum score: 0
Maximum score: 74
Average score: 2.1805555555555554
Median score: 0.0
Number of scores by letter grade:
A: 0
B: 0
C: 1
D: 0
F: 3
int scores[] = new int[stats.nextInt()];
here you take the first of your values and then you dont have it where you need it.
That is probably also the reason why your calculations are so messed up. you create an array of lenght 72 and use that lenght for the number of values.
Maybe you want to use a list instead. It allows you to add as many values as you want without specifying a number like you have to do when you use an array.
I am trying to write a program that allows a user to enter in a number of random grades they want to display along with the sum, average, minimum and maximum of the generated list. The grades range from 60 - 100.
The program is printing the min properly and the sum is adding up the previously generated sum along with the newly generated one. How can I change it so that it give the correct output for the minimum and so that is stops adding the previou sum to the new one? Any help would be appreciated.
The image link for the output shows the minimum output issue. The min should be 66.0 but it says 59.0.
output
import java.util.Scanner;
public class A03C
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("How many scores? ");
int howMany = input.nextInt();
double score = 0;
double sum = 0;
double average = 0;
double max = 0;
double min = 60;
while (howMany > 0)
{
for (int i = 1; i <= howMany; i++)
{
score = 60 + (int)(Math.random() * ((100 - 60) +1));
if (letterGrade(score));
sum += score++;
average = (sum/howMany);
if (score > max)
max = score;
if (score < max)
min = score;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
System.out.println("Max: " + (max - 1));
System.out.println("Min: " + (min - 1));
System.out.println("How many scores? ");
howMany = input.nextInt();
}
}
public static boolean letterGrade(double score)
{
if (score >= 92.0)
System.out.println(score + " is an A");
else if (score >= 83.0)
System.out.println(score + " is a B");
else if (score >= 75.0)
System.out.println(score + " is a C");
else
System.out.println(score + " is an F");
return false;
}
}
Just clean the sum variable after the execution, also I changed the return of the letterGrade to true:
import java.util.Scanner;
public class A03C {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many scores? ");
int howMany = input.nextInt();
double score = 0;
double sum = 0;
double average = 0;
double max = 0;
double min = 60;
while (howMany > 0) {
for (int i = 1; i <= howMany; i++) {
score = 60 + (int) (Math.random() * ((100 - 60) + 1));
if (letterGrade(score))
sum += score++;
average = (sum / howMany);
if (score > max)
max = score;
if (score < max)
min = score;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
System.out.println("Max: " + (max - 1));
System.out.println("Min: " + (min - 1));
System.out.println("How many scores? ");
sum = 0;
howMany = input.nextInt();
}
}
public static boolean letterGrade(double score) {
if (score >= 92.0)
System.out.println(score + " is an A");
else if (score >= 83.0)
System.out.println(score + " is a B");
else if (score >= 75.0)
System.out.println(score + " is a C");
else
System.out.println(score + " is an F");
return true;
}
}
Before continue, i definitely think that you should read #joe C comment.
As i understand you are a beginner, i will explain the changes i did.
import java.util.Scanner;
public class A03C
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("How many scores? ");
int howMany = = input.nextInt();
double score = 0;
double sum = 0;
double average = 0;
double max = 60;
double min = 100;
while (howMany > 0)
{
for (int i = 1; i <= howMany; i++)
{
score = 60 + (int)(Math.random() * ((100 - 60) +1));
letterGrade(score);
sum += score++;
average = (sum/howMany);
if (score > max)
max = score;
if (score < min)
min = score;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
System.out.println("Max: " + (max - 1));
System.out.println("Min: " + (min - 1));
System.out.println("How many scores? ");
howMany = input.nextInt();
}
}
public static void letterGrade(double score)
{
if (score >= 92.0)
System.out.println(score + " is an A");
else if (score >= 83.0)
System.out.println(score + " is a B");
else if (score >= 75.0)
System.out.println(score + " is a C");
else
System.out.println(score + " is an F");
}
}
Starting with the function letterGrade. Since the function always return false and just prints a sentence, you can replace boolean to void.
Another mistake you did was that if you want to find max and min, the variables shall take the inverse of you want. So the variable max shall take the minimum variable (60) and the maximum value (100).
Finally, in order to change variables max and min you must compare the new value to their current value. This is, to change min value, for example, you have to compare score with current min.
Hope it helps.
import java.util.Scanner;
public class Hw7Pr2 {
public static void main(String[] args) {
int[] grades = { 40, 55, 70, 58 };
System.out.println("best: ");
int best1 = best(grades);
System.out.print(best1);
// Print Grade
System.out.println("Grade: ");
char [] grade = (char[]) best(grades);
for (char i = 0; i < grade.length; i++) {
String output= " ";
output += "Student " + i + " score is " +
grades[i] + " and grade is " + grade + "\n";
}
}
private static char gradeLetter(int[] grades) {
char grade = 0;
for (int i = 0; i < grades.length; i++) {
if (grades[i] >= best(grades) - 10)
grade = 'A';
else if (grades[i] >= best(grades) - 20)
grade = 'B';
else if (grades[i] >= best(grades) - 30)
grade = 'C';
else if (grades[i] >= best(grades) - 40)
grade = 'D';
else
grade = 'F';
}
return grade;
}
public static int best(int[] grades) {
System.out.println("The best scores is: ");
int best = grades[0];
for (int i = 0; i < grades.length; i++) {
if (grades[i] > best)
best = grades[i];
}
return best;
}
}
I am trying to show the output like this
Student 1 score is 40 and grade is C
Student 2 score is 55 and grade is B
Student 3 score is 70 and grade is A
Student 4 score is 58 and grade is B
But I am having problems with printing the gradeLetter method.
Perhaps this is what you are trying to do :
public class Hw7Pr2 {
public static void main(String[] args) {
int[] grades = { 40, 55, 70, 58 };
int best = best(grades);
System.out.println("The best scores is: " + best);
// Print Grade
System.out.println("Grade: ");
for (int i = 1; i <= grades.length; i++) {
char grade = gradeLetter(grades[i-1], grades);
String output = " ";
output += "Student " + i + " score is " + grades[i-1] + " and grade is " + grade + "\n";
System.out.println(output);
}
}
private static char gradeLetter(int grade, int[] grades) {
char charGrade = 0;
if (grade >= best(grades) - 10)
charGrade = 'A';
else if (grade >= best(grades) - 20)
charGrade = 'B';
else if (grade >= best(grades) - 30)
charGrade = 'C';
else if (grade >= best(grades) - 40)
charGrade = 'D';
else
charGrade = 'F';
return charGrade;
}
public static int best(int[] grades) {
int best = grades[0];
for (int i = 1; i < grades.length; i++) { //will save a compare operation
if (grades[i] > best)
best = grades[i];
}
return best;
}
}
char [] grade = (char[]) best(grades);
Its a invalid part in your code. You can not convert an int to char[] array.
Just wanted to show you some other way of looking at it...
import java.util.Scanner;
public class Hw7Pr2 {
public static void main(String[] args) {
// Search the array for min, max and average
int[] grades = {40, 55, 70, 58};
gradeStats(grades);
}
static void gradeStats(int[] array) {
final int A = 90;
final int B = 80;
final int C = 70;
final int D = 60;
int minimumValue = 100;
int maximumValue = 0;
double gradeSum = 0;
// Get max and min grades
for (int i = 0; i < array.length; i++) {
if (array[i] < minimumValue) {
minimumValue = array[i];
}
if (array[i] > maximumValue) {
maximumValue = array[i];
}
}
// Get sum of grades
for (int i = 0; i < array.length; i++) {
gradeSum = gradeSum + array[i];
}
for (int i = 0; i < array.length; i++) {
if (array[i] >= A) {
System.out.println("Grade \"" + array[i] + "\" is an A.");
} else if (array[i] >= B) {
System.out.println("Grade \"" + array[i] + "\" is an B.");
} else if (array[i] >= C) {
System.out.println("Grade \"" + array[i] + "\" is an C.");
} else if (array[i] >= D) {
System.out.println("Grade \"" + array[i] + "\" is an D.");
} else {
System.out.println("Grade \"" + array[i] + "\" is an F.");
}
}
System.out.println("The lowest grade is a " + minimumValue + ".");
System.out.println("The highest grade is a " + maximumValue + ".");
System.out.printf("The sum is %d.\n", (int) gradeSum);
System.out.println("The average is " + gradeSum / array.length + ".");
}
}
I need to store the min and max values in an array given, then print them out with specific characters (+ for the maximum values, "-" for the minimum value, and "*" for all the rest).
I think I have most of it completed except for the storing values appropriately so that all the values are not "++++++++++...." like they currently are when printed out.
Any ideas? Help is greatly appreciated.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int numbers[] = new int[24];
int min = Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
int maxhour = 0;
int minhour = 0;
int total = 0;
char MAX = '+', MIN = '-', MIDDLE = '*';
char currentchar = 0;
for(int i=0; i< numbers.length; i++){
numbers[i] = keyboard.nextInt();
total = total + numbers[i];
if(numbers[i]<min){
min = numbers[i];
minhour = i;
currentchar = MIN;
}else if (numbers[i]>max){
max = numbers[i];
maxhour = i;
currentchar = MAX;
}
}
for(int i=0; i< numbers.length; i++){
System.out.print("Hour " + i + ":");
printTimes(currentchar, numbers[i]);
System.out.println("");
}
System.out.println("Largest Number of hits is : " + max + " at hour " + maxhour);
System.out.println("Average Number of hits is : " + (total/24) + " per hour");
System.out.println("Smallest Number of hits is : " + min + " at hour " + minhour);
}
public static void printTimes(char c, int times) {
if (times >= 70) {
for(int i=0; i< 69; i++){
System.out.print(c);
} System.out.print(">");
} else if (times < 70) {
for(int i=0; i< times; i++)
System.out.print(c);
}
}
}
Example of current output:
42 29 36 7 5 3 10 13 33 40 51 49
22 58 63 102 65 58 48 24 36 48 52 42
Hour 0:++++++++++++++++++++++++++++++++++++++++++
Hour 1:+++++++++++++++++++++++++++++
Hour 2:++++++++++++++++++++++++++++++++++++
Hour 3:+++++++
Hour 4:+++++
Hour 5:+++
Hour 6:++++++++++
Hour 7:+++++++++++++
Hour 8:+++++++++++++++++++++++++++++++++
Hour 9:++++++++++++++++++++++++++++++++++++++++
....
Largest Number of hits is : 102 at hour 15
Average Number of hits is : 39 per hour
Smallest Number of hits is : 3 at hour 5
Just change your last for:
for (int i = 0; i < numbers.length; i++) {
System.out.print("Hour " + i + ":");
if (numbers[i] == min)
currentchar = MIN;
else if (numbers[i] == max)
currentchar = MAX;
else
currentchar = MIDDLE;
printTimes(currentchar, numbers[i]);
System.out.println("");
}
I would update your code as follows -
public static void main(String[] args) {
int numbers[] = new int[24];
int total = 0;
System.out.println("Enter 24 integers please");
Scanner keyboard = null;
try {
keyboard = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = keyboard.nextInt();
total += numbers[i];
}
} finally {
keyboard.close();
}
Integer min = null;
Integer max = null;
int maxhour = 0;
int minhour = 0;
char MAX = '+', MIN = '-', MIDDLE = '*';
for (int i = 0; i < numbers.length; i++) {
if (min == null || numbers[i] < min) {
min = numbers[i];
minhour = i;
} else if (max == null || numbers[i] > max) {
max = numbers[i];
maxhour = i;
}
}
for (int i = 0; i < numbers.length; i++) {
char currentchar = MIDDLE;
if (i == minhour) {
currentchar = MIN;
} else if (i == maxhour) {
currentchar = MAX;
}
System.out.print("Hour " + i + ":");
printTimes(currentchar, numbers[i]);
System.out.println("");
}
System.out.println("Largest Number of hits is : "
+ max + " at hour " + maxhour);
System.out.println("Average Number of hits is : "
+ (total / 24) + " per hour");
System.out.println("Smallest Number of hits is : "
+ min + " at hour " + minhour);
}