I want to print how many number is greater than the average - java

I already print the total and average. However I can't print how many numbers is greater than average.
I think the problem is the number>= average, it seems like only adding the last input.
public static void main(String[] args) {
int i;
int number = 0;
double total=0;
double average=0;
int aboveaverage=0;
Scanner read = new Scanner (System.in);
for(i=1;i<9;i++){
System.out.print("Enter number " + i +": ");
number=read.nextInt();
if(number<0){
System.out.println("Invalid Input");
break;
}
total+=number;
}
if(number>=average){
aboveaverage+=1;
System.out.println("Greater than average is :" + aboveaverage);
}
average=total/8;
System.out.println("Print total : "+ total);
System.out.println("Print Average : " +average );
}
}

You will need to collect all numbers (by placing them in an int array), and iterate over all numbers, for example inside a for loop.
public static void main(String[] args) {
int i;
int number = 0;
int numberCount = 8;
int[] numberArray = new int[numberCount];
double total = 0;
double average = 0;
int aboveAverage = 0;
Scanner read = new Scanner (System.in);
for(i = 0; i < numberCount; i++){
System.out.print("Enter number " + (i + 1) + ": ");
number = read.nextInt();
if(number < 0){
System.out.println("Invalid input");
continue;
}
numberArray[i] = number;
total += number;
}
average = total / numberCount;
for(i = 0; i < numberCount; i++){
if(numberArray[i] > average) {
aboveAverage++;
}
}
System.out.println("Count of numbers greater than average: " + aboveAverage);
System.out.println("Print total: " + total);
System.out.println("Print average: " + average);
}

Related

How to set a limit in a for loop?

I have to create a program to calculate the average of each students' scores. I managed to do that but how can I limit the score to be only between 0 to 100? I've searched other questions and many shows to put while statement. The problem is that I don't know where to add the while. So here's the code:
import java.util.Scanner;
public class AverageScore {
public static void main(String[] args) {
int x; // Number of students
int y; // Number of tests per student
int Score = 0; //Score of each test for each student
double Average = 0; //Average score
double Total = 0; //Total score
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the number of students: ");
x = keyboard.nextInt();
System.out.println("Please enter the amount of test scores per student: ");
y = keyboard.nextInt();
for (int z = 0; z < x; z++)
{
System.out.println("Student " + (z + 1));
System.out.println("------------------------");
for (int g=0; g < y; g++)
{
System.out.print("Please enter score " + (g + 1) + ": ");
Total += new Scanner(System.in).nextInt();
Total += Score;
Average = (Total/y);
}
System.out.println("The average score for student " + (z + 1) + " is " + Average);
System.out.println(" ");
Total= 0;
}
keyboard.close();
}
}
If there is any other ways please do state. Thanks in advance.
import java.util.Scanner;
public class AverageScore {
public static void main(String[] args) {
int x; // Number of students
int y; // Number of tests per student
int Score = 0; //Score of each test for each student
double Average = 0; //Average score
double Total = 0; //Total score
double Input = 0; **//Add this in your variable**
boolean Valid = false; **//Add this in your variable**
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the number of students: ");
x = keyboard.nextInt();
System.out.println("Please enter the amount of test scores per student: ");
y = keyboard.nextInt();
for (int z = 0; z < x; z++)
{
System.out.println("Student " + (z + 1));
System.out.println("------------------------");
for (int g=0; g < y; g++)
{
System.out.print("Please enter score " + (g + 1) + ": ");
Input = new Scanner(System.in).nextInt();
//validation of your input from 0 to 100
if(Input>=0&&Input<=100)
{
Valid = true;
}
//enter while loop if not valid
while(!Valid)
{
System.out.println("");
System.out.print("Please enter a valid score " + (g + 1) + ": ");
Input = new Scanner(System.in).nextInt();
if(Input>=0&&Input<=100)
{
Valid = true;
}
}
Valid = false; //reset validation;
Total += Input;
Average = (Total/y);
}
System.out.println("The average score for student " + (z + 1) + " is " + Average);
System.out.println(" ");
Total= 0;
}
keyboard.close();
}
}
An easy way to go about this would be to put the user-input prompt inside of a while loop, and only break out once you've verified that the grade is valid:
Scanner scanner = new Scanner(System.in);
int score;
while (true) {
System.out.print("Please enter score " + (g + 1) + ": ");
score = scanner.nextInt();
if (score >= 0 && score <= 100) {
break;
}
System.out.println("Please enter a valid score between 0 and 100!");
}
Total += score;
Remember to close your Scanners to avoid memory leaks!

Java Change Prompt Order

I am currently working on a java program that has to do with taking classes and the amount of credits for each class. I have everything set up how I need it, except the order.
I would like it to ask for a class, then how many credits that class is, then ask for the next class, and those credits, and so on. Right now, it will ask for all of the classes, then all of the credits. Here's the code I have:
//Jake Petersen
import java.util.Scanner;
public class test1{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("How many courses are you going to list?");
int courses = Integer.parseInt(scan.nextLine());
String courseArray[] = new String[courses];
for (int i = 0; i < courseArray.length; i++){
System.out.println("Please enter a course:");
courseArray[i] = scan.nextLine();
}
int creditArray[] = new int[courses];
for (int i = 0; i < creditArray.length;) {
System.out.println("Please enter how many credits "+ courseArray[i] + " is:");
int input = scan.nextInt();
if (input >= 1 && input <= 4) {
creditArray[i++] = input;
}
}
int sum = 0;
for (int i : creditArray){
sum += i;
}
for (int i = 0; i < courseArray.length; i++) {
System.out.print(courseArray[i] + " is a " + creditArray[i] + " credit class. \n");
}
print(sum);
}
public static void print(int sum){
if(sum >= 12 && sum <= 18){
System.out.println("You are taking " + sum + " total credits, which makes you a full time student.");
}else if(sum < 12){
System.out.println("You are taking " + sum + " total credits, which makes you not a full time student.");
}else{
System.out.println("You are taking " + sum + " total credits, which means you are overloaded");
}
}
}
Do all the prompts in a single for loop:
import java.util.Scanner;
public class test1{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print("How many courses are you going to list?");
int courses = Integer.parseInt(scan.nextLine());
String courseArray[] = new String[courses];
int creditArray[] = new int[courses];
for (int i = 0; i < courseArray.length; i++){
System.out.print("Please enter a course:");
courseArray[i] = scan.nextLine();
System.out.print("Please enter how many credits "+ courseArray[i] + " is:");
String credits = scan.nextLine();
int input = Integer.parseInt(credits);
if (input >= 1 && input <= 4) {
creditArray[i] = input;
}
else {
creditArray[i] = 0;
}
} int sum = 0;
for (int i : creditArray){
sum += i;
}
for (int i = 0; i < courseArray.length; i++) {
System.out.print(courseArray[i] + " is a " + creditArray[i] + " credit class. \n");
}
print(sum);
}
public static void print(int sum){
if(sum >= 12 && sum <= 18){
System.out.println("You are taking " + sum + " total credits, which makes you a full time student.");
}else if(sum < 12){
System.out.println("You are taking " + sum + " total credits, which makes you not a full time student.");
}else{
System.out.println("You are taking " + sum + " total credits, which means you are overloaded");
}
}
}
Of course this assumes that the 2 arrays have the same size. Perhaps you want to prompt for a class count first, to know how large to make the arrays, or grow them dynamically.

Java addition program which keeps addup the numbers you input ing until you enter a zero. Average of SumPositive and SumNegative

In this java program you (the user) keeps entering numbers until you enter a zero, which is when the list terminates. It will compute the sum of positive even & sum of odd even and negative numbers. It will also compute the average.
I'm stuck at the part to get the average of sum of even positive numbers, sum of odd positive numbers, and sum of negative numbers.
import java.util.*;
class sumPositiveAverage {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println ("Enter numbers. List terminates when you enter a zero.
Enter a zero when you want to begin the addition.");
int a = sc.nextInt();
int esum=0;
int osum=0;
int nsum=0;
while (a !=0)
{
if (a>0)
{
if (a%2==0)
{
esum = esum+a;
}// end of 3rd innermost if statement
else
{
osum = osum+a;
}// end of 3rd else statement
}//end of 2nd middle if-else-loop
else if (a<0)
{
nsum=nsum+a;
}//end of 2nd middle else statement
}//end of while loop
System.out.println ("The sum of even positive numbers is "+esum);
System.out.println ("The sum of odd positive numbers is "+osum);
System.out.println ("The sum of negative numbers is "+nsum);
}//end of main
}//end of class2
import java.util.*;
public class sumPositiveAverage {
public static void main(String[] args)
{
int sum_even = 0;
int sum_odd = 0;
int sum_negative = 0;
int sum_positive = 0;
int count_even = 0;
int count_odd = 0;
int count_negative = 0;
int count_positive = 0;
double avg_sum_even = 0;
double avg_sum_odd = 0;
double avg_sum_negative = 0;
double avg_sum_positive = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
int data = 0;
do
{
System.out.print("Type in a positive or negative number and press enter key, if 0 is entered program stops: ");
data = input.nextInt();
if(data > 0){
if(data%2 == 0){
//even number
sum_even = sum_even + data;
count_even++;
}else{
//odd
sum_odd = sum_odd + data;
count_odd++;
}
sum_positive = sum_positive + data;
count_positive++;
}else if(data < 0) {
//negative number
sum_negative = sum_negative + data;
count_negative++;
}
}
//Stops if the value of data is ZERO(0) and continues if it's not
while(data != 0);
//here means zero has been entered
if(count_positive > 0) { avg_sum_positive = (double)sum_positive/count_positive; }
if(count_negative > 0) { avg_sum_negative = (double)sum_negative/count_negative; }
if(count_even > 0) { avg_sum_even = (double)sum_even/count_even; }
if(count_odd > 0) { avg_sum_odd = (double)sum_odd/count_odd; }
System.out.println("Sum of Positive Numbers = " + sum_positive);
System.out.println("Sum of negative Numbers = " + sum_negative);
System.out.println("Sum of odd Numbers = " + sum_odd);
System.out.println("Sum of even Numbers = " + sum_even);
System.out.println("Count of Positive Numbers = " + count_positive);
System.out.println("Count of negative Numbers = " + count_negative);
System.out.println("Count of odd Numbers = " + count_odd);
System.out.println("Count of even Numbers = " + count_even);
System.out.println("Average of Positive Numbers = " + avg_sum_positive);
System.out.println("Average of negative Numbers = " + avg_sum_negative);
System.out.println("Average of odd Numbers = " + avg_sum_odd);
System.out.println("Average of even Numbers = " + avg_sum_even);
}//closing main
} // closing class

How do I create a counter to count the number of people who had the maximum salary entered?

import java.util.Scanner;
public class Lab4a{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int numemployees, max, freq;
System.out.print("Enter number of employees: ");
numemployees = keyboard.nextInt();
System.out.println("Enter salary for " + numemployees + " employees:");
int[] salaries = new int [numemployees];
max = salaries[0];
for (int i=0; i < salaries.length; i++){
salaries[i] = keyboard.nextInt();
if (salaries[i] > max)
max = salaries[i];
}
System.out.println("Maximum salary is " + max);
System.out.println(" employees received " + max);
}
So the output for this would be
Enter number of employees: 3
Enter salary for 3 employees:
10000
15000
15000
Maximum salary is 15000
employees received 15000
I WANT IT TO SAY, "2 employees received 15000". That's all I need for it to do. Please help me how to establish that counter using another loop... as simple as possible! Thanks!
Amend your loop like this:
max = salaries[0];
freq = 1;
for (int i=0; i < salaries.length; i++){
salaries[i] = keyboard.nextInt();
if (salaries[i] > max) {
freq = 1;
max = salaries[i];
} else if (salaries[i] == max) {
++freq;
}
}
System.out.println(freq + " employees received " + max);
int count = 0;
for (int i=0; i < salaries.length; i++){
if (salaries[i] == max)
count++;
}
System.out.println(count+" employees received "+max);
Try this out
int count =0;
System.out.println("Maximum salary is " + max);
System.out.println(" employees received " + max);
for(int i=0;i< salaries.length ;i++)
{
if(salaries[i] == max)
count++;
}
if(count>1)
{
System.out.println(count+" employees received " + max);
}
Here you go -
package com.walmart.services;
import java.util.Scanner;
public class Lab4a {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int numemployees, max, freq = 0;
System.out.print("Enter number of employees: ");
numemployees = keyboard.nextInt();
System.out.println("Enter salary for " + numemployees + " employees:");
int[] salaries = new int[numemployees];
max = 0;
for (int i = 0; i < salaries.length; i++) {
salaries[i] = keyboard.nextInt();
if (salaries[i] > max) {
freq = 1;
max = salaries[i];
} else if (salaries[i] == max) {
freq++;
}
}
System.out.println("Maximum salary is " + max);
System.out.println(" employees received " + freq);
}
}

My programs print statement is wrong

When i run my program and choose a number between 0 and 100, it prints my answer wrong.
Java console
----jGRASP exec: java TestScores
How many tests do you have? 3
Enter grade for Test 1: 80
Enter grade for Test 2: 80
Enter grade for Test 3: 80
The average is: 26.666666666666668The average is: 53.333333333333336The average is: 80.0
----jGRASP: operation complete.
import java.util.Scanner;
public class TestScores {
public static void main(String[] args)
{
int numTests = 0;
double[] grade = new double[numTests];
double totGrades = 0;
double average;
int check = 1;
Scanner keyboard = new Scanner(System.in);
System.out.print("How many tests do you have? ");
numTests = keyboard.nextInt();
grade = new double[(int) numTests];
for (int index = 0; index < grade.length; index++)
{
System.out.print("Enter grade for Test " + (index + 1) + ": ");
grade[index] = keyboard.nextDouble();
if (grade[index] < 0 || grade[index] > 100)
{
try
{
throw new InvalidTestScore();
}
catch (InvalidTestScore e)
{
e.printStackTrace();
}
break;
}
}
for (int index = 0; index < grade.length; index++) {
totGrades += grade[index];
average = totGrades / grade.length;
System.out.print("The average is: " + average);
}
}
}
public class InvalidTestScore extends Exception
{
public InvalidTestScore()
{
super(" Error: Enter a number between 0 and 100");
}
}
You print the average inside the loop that calculates the average.
Print it only outside the loop.
You should calculate sum in loop and then (after the loop) divide it by the number of elements.
I move the statement which calculates the sum from inside the loop to the outside, that works.
My new code is.
import java.util.Scanner;
public class TestScores
{
public static void main(String[]args)
{
int numTests = 0;
double[] grade = new double[numTests];
double totGrades = 0;
double average;
int check = 1;
Scanner keyboard = new Scanner(System.in);
System.out.print("How many tests do you have? ");
numTests = keyboard.nextInt();
grade = new double[(int) numTests];
for (int index = 0; index < grade.length; index++)
{
System.out.print("Enter grade for Test " + (index + 1) + ": ");
grade[index] = keyboard.nextDouble();
if (grade[index] < 0 || grade[index]> 100)
{
try
{
throw new InvalidTestScore();
}
catch (InvalidTestScore e)
{
e.printStackTrace();
}
break;
}
}
for (int index = 0; index < grade.length; index++)
{
totGrades += grade[index];
}
average = totGrades/grade.length;
System.out.print("The average is: " + average);
}
}
public class InvalidTestScore extends Exception
{
public InvalidTestScore()
{
super(" Error: Enter a number between 0 and 100");
}
}
You can close my post.

Categories

Resources