I'm having trouble making the code display the highest average of the four averages, I seem to only get the last average score and that fills in for the highest average which it is not the highest score.
Here is my code:
import java.io.*;
import java.util.*;
class Bowling Scores
{
public static void main (String args[])
{
Scanner keyboard = new Scanner(System.in);
int G =0;
int scores = 0;
double avg = 0;
double highest_average=0;
int times = 0;
int players = 4;
for(int i =1; i<=players; i++) {
System.out.println("Player " + i);
if (avg > highest_average)
highest_average= avg;
do
{
System.out.println("Enter your bowling score:");
G = keyboard.nextInt();
scores += G;
times++;
}
while((G !=-1)); // -1 is what makes the loop end for the current player, a total of four players will get an average after -1 is inputted
scores += + 1;
times = times - 1;
avg = (scores)/(times);
System.out.printf("Average= %.2f\n", avg);
scores =0; times =0;
System.out.println("");
}
System.out.println("Highest average:" + highest_average);
}
}
Here is a sample run of the code:
Player 1
Enter your bowling score:
300
Enter your bowling score:
222
Enter your bowling score:
-1
Average= 261.00
Player 2
Enter your bowling score:
210
Enter your bowling score:
211
Enter your bowling score:
300
Enter your bowling score:
-1
Average= 240.00
Player 3
Enter your bowling score:
233
Enter your bowling score:
-1
Average= 233.00
Player 4
Enter your bowling score:
112
Enter your bowling score:
-1
Average= 112.00
Highest average:112.0 // this is not the highest score.
if (avg > highest_average) {
highest_average= avg;
This code should be in your for-loop, otherwise the highest average never gets updated.
You never update Highest Avg for each player, only for the last player. That's why you're always using the last person's avg.
Just move your if statement inside the for loop and it should work fine. Make sure to leave your print statement outside the loop so that you only print the highest avg once at the end.
for (int i = 1; i <= players; i++) {
System.out.println("Player " + i);
do {
System.out.println("Enter your bowling score:");
G = keyboard.nextInt();
scores += G;
times++;
} while ((G != -1)); // -1 is what makes the loop end for the current player, a total of four players will get an average after -1 is inputted
scores += +1;
times = times - 1;
avg = (scores) / (times);
System.out.printf("Average= %.2f\n", avg);
scores = 0;
times = 0;
System.out.println("");
if (avg > highest_average) {
highest_average = avg;
}
}
System.out.println("Highest average:" + highest_average);
Related
Basically, I have ask the user to input two exam scores (which are doubles). If the two exam scores average out to more than 90, then a message is outputted on the screen that they got an A, and so on for other average scores.
No matter what combination I enter, it always outputs that the user has obtained a 'C' grade even though they did not.
What is the issue here?
There were no compiler errors.
package test;
import java.util.Scanner;
class test3 {
public static void main(String args[]) {
Scanner zino = new Scanner(System.in);
double score1 = 0;
double score2 = 0;
double average = 0;
average = (score1 + score2) / 2 ;
System.out.println("Enter score 1 ");
score1 = zino.nextDouble();
System.out.println("Enter score 2 ");
score2 = zino.nextDouble();
if(average > 90)
{
System.out.println(average);
System.out.println("You got an A!");
}
else if(average > 80)
{
System.out.println(average);
System.out.println("You got a B.");
}
else
{
System.out.println(average);
System.out.println("You got a C.");
}`
}
}
If the user inputs two scores that are both 100, the output should be:
100
You got an A!
This is what I am getting:
0.0
You got a C.
Place the code average=(score1+score 3)/2; after you finished getting user inputs.
Your code should looks like this :
System.out.println("Enter score 1 ");
score1 = zino.nextDouble();
System.out.println("Enter score 2 ");
score2 = zino.nextDouble();
average=(score1+score 3)/2;
Your problem is that you're calculating the average before you're reading the values of the two scores.
average = (score1 + score2) / 2 ;
When you run this line, score1 and score2 are both zero, so you're setting average to ( 0 + 0 ) / 2, which of course is zero.
Later in your program, you have
score1 = zino.nextDouble();
...
score2 = zino.nextDouble();
which sets score1 and score2 to the values entered by the user. But you never calculate average again - so it stays at zero.
What you need to do is move the line average = (score1 + score2) / 2; downwards, so that it runs after the two lines that set score1 and score2.
I'm doing a project for my programming class and I'm stuck. I've written the code but I can't figure out how to implement the min, max and average into the class. We haven't learned about arrays yet and I just need a good nudge in the right direction. Please help!
The exercise reads: Write an application that allows a user to enter any number of student test scores until the user enters 999. If the score entered is less than 0 or more than 100, display an appropriate message and do not use the score. After all the scores have been entered, display the number of scores entered, the highest score, the lowest score, and the arithmetic average.
import java.util.Scanner;
public class TestScoreStatistics {
public static void main(String[] args)
{
double score;
double max;
double min;
double average;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter in test scores: ");
score = keyboard.nextDouble();
while(score >= 0 || score <= 100) {
if(score >= 101 || score <= 0.9)
System.out.println("The score must be between 0 - 100");
else
System.out.println("Please enter in test scores or type 999 to calculate final scores: ");
score = keyboard.nextDouble();
if(score == 999)
System.out.println("The maximum score is - " + max + ".");
System.out.println("The minimum score is - " + min + ".");
System.out.println("The average score is - " + average + ".");
}
}
}
I'm not looking for the direct answer, I just want confirmation that I'm using the right type of loop and what code should I use for the math part. Thank you in advance.
To calculate an average, you just need two variables, one for the sum of all the scores, and one for the number of scores. That shouldn't require arrays. Also, the min and max score values can be updated each time a score is input.
This is my first Java program. It is supposed to allow a user to enter int grades between 0 and 100. If the user enters a negative value, data entry ceases and statistics are displayed. I cannot incorporate static methods, math, or arrays.
I am having an issue with finding the minimum grade, minGrade. Regardless of what values are entered when the program is running, minGrade always results in zero. I have been tinkering with this for a while now to no avail.
The other issue I am having is that when I run the program, and I enter a bunch of int, but then enter some alphabet letters to test the error-checking, the program parses the user twice, instead of once.
"Please enter a numeric grade between 0 and 100, inclusive, and press Enter:"
The respective code is:
import java.util.Scanner;
public class Grades {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the Course Code and and press Enter:");
String courseCode = keyboard.nextLine();
System.out.println("You entered: " + courseCode + "\n");
int grade = 0;
int numberOfGrades = 0;
int maxGrade = 0;
int minGrade =0;
double avgGrade = 0;
int sumGrades = 0;
int sentinel = 0;
do
{
**System.out.println("Please enter a numeric grade between 0 and 100, inclusive, and press Enter:");**
while(!keyboard.hasNextInt())
{
System.out.println("Please enter a numeric grade between 0 and 100, inclusive, and press Enter:");
keyboard.nextLine();
}
grade = keyboard.nextInt();
if((grade <=100) && (grade >= 0))
{
numberOfGrades++;
sumGrades += grade;
sentinel = 0;
if(maxGrade < grade)
maxGrade = grade;
**if(minGrade > grade)
minGrade= grade;**
}
else if(grade > 100)
{
System.out.println("The entered number was greater than 100. Please enter a number between 0 and 100 inclusive, "
+ "or input a negative number to exit Grade entry");
sentinel = 0;
}
else if(grade <0)
{
grade = 0;//maybe?
sentinel = -1;
}
}
while((grade >100) || (sentinel == 0));
avgGrade = (sumGrades/numberOfGrades);
System.out.println("You entered: " + "\ngrade: " + grade + "\n" + "sentinel: "+ sentinel +
"\nSum of Grades: " + sumGrades +"\nNumber of Grades: "+ numberOfGrades +"\nAverage Grades: " + avgGrade
+ "\nMaxium Grade: "+ maxGrade+"\nMinimum Grade: "+minGrade);
}
}
Any input on this, the form of my code for my first java program, or anything else would be greatly appreciated.
The last issue I am having is that the average grade always has a tenth place of zero. How can I get the tenth place to not be zero and the actual average amount?
Your problem with minGrade is that you initialize it to 0. This is already the min value, hence no other values will be less than this. (You in fact ensure this even more so by setting negative grades to 0.) Initialize it to the max value (100).
It looks like your "double prompt" issue is a common one with nextLine and nextInt. The solution seems to be using only nextLine and then parsing the return value of that for an Integer object. (And printing out your "invalid input" text on a NumberFormatException.
Finally, your avgGrade is always xx.0 as you are dividing two integers, which always gives an integer back. You need to cast one to a double to get a double back:
avgGrade = ((double)sumGrades/numberOfGrades);
minGrade will never be set to anything other than 0 because 0 will never be greater than the grade entered, so this check fails if(minGrade > grade). Try initializing minGrade to Integer.MAX_VALUE.
I need to write a program in Java that computes the average score for 4 students. The student will put in their 4 scores, and once they are done they will input -1 to compute the average. Once this is done the program needs to move onto student 2 and so on. At the end it is supposed to display the highest average from the 4 student average test scores. Here is what it should look like when it is run:
Student 1
Enter your score: 100
Enter your score: 90
Enter your score: 80
Enter your score: 70
Enter your score: -1 * once the student enters -1 it should compute average
Average Score = 85.
Student 2
Enter your score: 90
ETC
ETC
The problem with my code is that the average is only correct for the first student. When I input -1 to get the average for the second student, the calculation is incorrect. We are only allowed to use loops. The only hints I was given were that we are supposed to write an outer loop that iterates 4 times, write an inner loop that loops as long as the student has scores to enter, inside the inner loop prompt the user to enter their last score or -1 to compute average. I don't want you guys to do the project for me but to just set me in the right direction. I feel like I am not using the right loop.
import java.util.Scanner;
public class TestScore
{
public static void main(String[]args)
{
double score = 0;
double totalScore = 0;
double count = 0;
double average = 0;
Scanner input = new Scanner(System.in);
System.out.println("Student 1");
System.out.printf("Enter Your Score: ");
score = input.nextDouble();
while (score != -1){
System.out.printf("Enter Your Score: ");
totalScore = totalScore + score;
score = input.nextDouble();
count++;
average = totalScore / count;
if (score == -1){
System.out.printf("Average Score = %.2f\n ",average);
count = 0;
score = 0;
totalScore = 0;
average = 0;
System.out.println("Student 2");
System.out.printf("Enter Your Score: ");
score = input.nextDouble ();
count++;
average = totalScore / count;
}
}
}
}
You haven't explicitly asked a question so I'll try and comply to the "set me in the right direction" part.
I'd suggest re-formatting the loop structure to a cleaner one, like this:
double total;
for(int student = 1; student <= 4; student++) {
System.out.printf("Student %d\n", student);
double sum = 0, count = 0;
while(true) {
System.out.printf("Enter your score: ");
double input = scanner.nextDouble();
if(input == -1) break;
sum += input;
count++;
}
total += sum;
System.out.printf("Average: %.2f\n", sum / count);
}
System.out.printf("Total: %.2f\n", total);
Hope that's enough to give you some pointers.
edit: forgot to take care of total
So, you wish to iteratively go through all the input and just remember the maximum one. Make an integer variable max and after each student, just change it if needed. (It's zero by default jn Java)
As for the calculation for each student, you shouldn't be checking for the failed " score != - 1" condition in each iteration. Instead, you should do the final calculations after the while loop. (average, possible update of the maximum, resetting the variables, etc. )
You also need the outer loop (in the stated code, these calculations are done for one student only) which you would control in a different manner.
Also, if you need to use only 4 grades, you might want to consider using the for loop.
You can try with this :D
public static void main (String[] args) throws java.lang.Exception
{
double average = 0;
double i = 0;
int student = 0;
boolean flag = true;
Scanner input = new Scanner(System.in);
while(flag)
{
System.out.printf("Student: ");
System.out.println(student);
System.out.print("Enter Your Score: ");
double score = input.nextDouble();
if(score!=-1){
average=average+score;
i=i+1;
}
if(score==-1){
System.out.printf("Average: ");
System.out.println(average/i);
//reset values
average = 0;
i = 0;
student=student+1;
}
if(score==-2){
//you need break the while in some moment.
flag = false;
}
}
}
I'm trying to figure out how to implement a total of my SCORES and VOTES which I have done no problem. Where I'm getting confused and can't seem to solve is how I would go back through my array of Contestants and get their percent of the total SCORES or VOTES. e.g player 1 has a score of 10 out of a total score of 30, player 2 has a score of 15/30 ect... a running average is not a problem its a real total average I can't seem to wrap my head around. Any advice would be terrific.
package contestscore;
import java.util.*;
public class ContestScore {
public static void main(String[] args) {
// TODO code application logic here
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter number of contesants: ");
int numContest = keyboard.nextInt();
System.out.println("Enter contestants DANCE SCORE and NUMBER OF VOTES EARNED: ");
int[] danceScore = new int[numContest+1];
int[] numVotes = new int[numContest+1];
double avgDanceScore = 0.0;
double avgVoteScore = 0.0;
double totalDanceScore = 0.0;
int numContestIndex = 0;
double avgContestDanceScore = 0.0;
int totalVotes = 0;
double avgContestVote = 0.0;
for(numContestIndex = 1; numContestIndex <= numContest; numContestIndex++)
{
danceScore[numContestIndex] = keyboard.nextInt();
numVotes[numContestIndex] = keyboard.nextInt();
System.out.println("DANCE SCORE: "+danceScore[numContestIndex]);
System.out.println("NUMBER OF VOTES: "+numVotes[numContestIndex]);
totalDanceScore = totalDanceScore + danceScore[numContestIndex];
//avgContestDanceScore = danceScore[numContestIndex] /totalDanceScore;
System.out.println("TOTAL DANCE SCORE: "+totalDanceScore);
totalVotes = totalVotes + numVotes[numContestIndex];
//avgContestVote = numVotes[numContestIndex] / totalVotes;
System.out.println("TOTAL CONTESTANT VOTE SHARE: "+totalVotes);