I just found that when I calculate the average for the second user, I get a different result. For example, if I add this number for the first user 10,10,10,20,20. I get 14.0 which is correct. But when I enter the same number for the second user I get 28.0? any ideas?
import java.util.Scanner;
public class midterm
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
double average=0;
int count = 0;
int salary_annually = 0;
for(int employee =1; employee <= 2; employee++)
{
System.out.println("Employee: " + employee);
for(int year=1; year <= 5; year++)
{
System.out.println("Please Enter the Salary for Year: " + year);
salary_annually = kb.nextInt();
sum += salary_annually ;
average = (sum) / 5.0;
if (min >= salary_annually)
{
min = salary_annually;
}
if (max <=salary_annually)
{
max = salary_annually;
}
}
System.out.println("The average is " + average);
System.out.println("The higher number " + max);
System.out.println("The the lowest number " + min);
}
}
}
sum = 0; // this before 2nd loop
int max = Integer.MIN_VALUE; // these too.
int min = Integer.MAX_VALUE;
for(int year=1; year <= 5; year++)
{
.
.
.
}
average = (sum) / 5.0; // this after 2nd loop
In your solution you are calculating average 5 times inside the loop! and you only get advantage from the fifth time! must be after the loop. average = (sum) / 5.0; after the loop.
You need to zero sum at the end of the loop. As it is, now it just keeps increasing and increasing as the old value is kept in memory, when coming to the next iteration.
Related
package HW2_Min_Max;
import java.util.Scanner;
public class HW2_Min_Max {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Please input a positive interger that indicates number of positive intergers ");
int number = myScanner.nextInt();
while (number <= 0) {
System.out.println("Please input interger");
number = myScanner.nextInt();
}
int i=1; //i is to store current iteration
int sum=0; //sum is to store sum of the input
int x; //x is to store the user input
while (i <= number){
System.out.println("Please input a positive interger ");
x = myScanner.nextInt();
sum = sum + x;
i++;
}
int average = sum/number;
System.out.println("The average is " + average);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
if (number < min){
min = number;
}
if (number > max) {
max = number;
}
System.out.println("The minimum value is " + min);
System.out.print( "and the maximum value is" + max);
}
}
}
1.^ this is where i am getting my problem, on the very last brace in Netbeans i am getting an error that says "class, interface, or enum expected" but i have no idea why. Excuse my ignorance as I am a very fresh beginner with java, let alone programming.
For the first part, I would suggest you use a do-while loop to test for the number of numbers. Like,
int number;
do {
System.out.println("Please input a positive integer that "
+ "indicates number of positive integers ");
number = myScanner.nextInt();
} while (number <= 0);
Then you need to set min and max in your loop (or store all the values you read). I would prefer Math.max and Math.min. I would also count from 0. Like,
int i = 0; // i is to store current iteration
int sum = 0; // sum is to store sum of the input
int x; // x is to store the user input
int max = Integer.MIN_VALUE; // store the max user input
int min = Integer.MAX_VALUE; // store the min user input
while (i < number) {
System.out.println("Please input a positive integer ");
x = myScanner.nextInt();
if (x > 0) { // make sure it's a positive integer
min = Math.min(min, x);
max = Math.max(max, x);
sum += x;
i++;
}
}
int average = sum / number;
System.out.println("The average is " + average);
System.out.println("The minimum value is " + min);
System.out.println("and the maximum value is " + max);
Its working fine now. I have made some changes in it. Kindly consider it. I hope it would help.
Scanner myScanner = new Scanner(System.in);
System.out.println("Please input a positive interger that indicates number of positive intergers ");
int number = myScanner.nextInt();
while (number <= 0) {
System.out.println("Please input interger");
number = myScanner.nextInt();
}
int[] arr = new int[number]; // Store values in array
int i=0; //i is to store current iteration
int sum=0; //sum is to store sum of the input
int x; //x is to store the user input
int max = 0;
int min = 0;
for(i=0;i<number;i++){
System.out.println("Please input a positive interger ");
arr[i] = myScanner.nextInt();
sum = sum + arr[i];
}
int average = sum/number;
System.out.println("The average is " + average);
// Put initial values in min and max for comparing
min =arr[0];
max = arr[0];
for(i=1;i<number;i++)
{
// Compare if values is less than arr[i]
if (arr[i] < min){
min = arr[i];
}
// Compare if values is greater than arr[i]
if (arr[i] > max) {
max = arr[i];
}
}
System.out.print("The minimum value is " + min);
System.out.println( " and the maximum value is " + max);
}
}
Here is the output:
Not sure where to begin honestly, I was able to find average using the user's input but can't seem to figure out the largest number or the smallest number from the numbers entered. Is there a method I can use? Any help would be great. Thank you.
EDIT: Somewhat figured it out. However the answers always seem to be largest number = 2.1478... or Smallest number = -2.1478...
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of grades: ");
double random = input.nextDouble();
double min = Integer.MIN_VALUE;
double max = Integer.MAX_VALUE;
double total=0;
int count = 1;
while (count < random+1) {
System.out.println("Enter grade " + count + ":");
double somenumber = input.nextDouble();
total+=somenumber;
count++;
if (somenumber > max){
max = somenumber;
}
if (somenumber < min){
min = somenumber;
}
}
System.out.println("Total is " + total);
System.out.println("Average is " + (total/random));
System.out.println("Largest number is " + max);
System.out.println("Smallest number is" + min);
}
}
You want some code that looks like
double min = Double.MAX_VALUE;
double max = 0.00; // assume use positive numbers
while (count < random+1) {
System.out.println("Enter grade " + count + ":");
double somenumber = input.nextDouble();
total+=somenumber;
count++;
max = Math.max (somenumber, max);
min = Math.min (somenumber, min);
}
EDIT Updated to use Double instead and now uses your original loop condition.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double smallest = Double.POSITIVE_INFINITY; //Initialize to largest int possible
double largest = Double.NEGATIVE_INFINITY; //Initialize to smallest int possible
System.out.println("Enter the number of grades: ");
double random = input.nextDouble();
int count = 1;
while (count < random + 1) {
System.out.print("Input Number: ");
Double x = input.nextDouble();
if (x < smallest) {
smallest = x;
}
if (x > largest) {
largest = x;
}
count++;
}
System.out.println("Smallest: " + smallest + ", Largest: " + largest);
}
}
Sample run:
run:
Enter the number of grades:
5
Input Number: 2.5454
Input Number: 3.454
Input Number: -100.54
Input Number: 5687
Input Number: 579873.5654
Smallest: -100.54, Largest: 579873.5654
BUILD SUCCESSFUL (total time: 15 seconds)
Thank you for the tips everyone. However, when I ran my program. I wasn't able to get the proper answers for the max and min value.
EDIT: SOLVED. THANK YOU ALL WHO CONTRIBUTED. <33
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of grades: ");
double random = input.nextDouble();
double min = Integer.MIN_VALUE;
double max = Integer.MAX_VALUE;
double total=0;
int count = 1;
while (count < random+1) {
System.out.println("Enter grade " + count + ":");
double somenumber = input.nextDouble();
total+=somenumber;
count++;
if (somenumber > max){
max = somenumber;
}
if (somenumber < min){
min = somenumber;
}
}
System.out.println("Total is " + total);
System.out.println("Average is " + (total/random));
System.out.println("Largest number is " + max);
System.out.println("Smallest number is" + min);
}
}
Largest number is 2.14748 Smallest number is -2.147478...That's what was printed...Am I making a arthimetic error?
Above while loop...
double max = 0;
double min = Double.MIN_VALUE;
In loop after you set some number...
if (someNumber > max){
max = someNumber;
}
if (someNumber < min){
min = someNumber;
}
Np glad it helped :) you should consider using a for loop instead of a while loop and declare someNumber outside of the loop like so:
double someNumber;
for (int i = 1; i <= random; i++){
//...
}
To make it even cleaner you could replace i <= random in the for loop with
i <= readNumGrades(input);
private double readNumGrades(Scanner in){
System.out.println("Enter the number of grades");
return in.nextDouble();
}
So you no longer need the variables count and random
I'm new to Java, and am attempting to do an assignment...but am feeling lost right about now. We haven't been introduced to arrays yet, so our vocabulary is a bit limited.
1.) Modify your current code so that it will handle multiple years of rainfall data. Give the option to the user to supply the actual number of years they want to enter in the program for evaluation. This program must output the year (i.e. Year 1, Year 2, etc.) for most rainfall and the year with the least amount of total rainfall.
Below is my code:
import java.util.Scanner;
public class Test2OF
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double[] rainfall = new double[4];
double totalRainfall = 0.0;
double max = 0, min = 0;
int year = scan.nextInt();
int maxQuarter = 1;
int minQuarter = 1;
// Prompt user for the number of years
System.out.println("Enter the number of years: ");
year = scan.nextInt();
for (int i=0; i < year*4; i++)
{
System.out.print("Enter rainfall for quarter " + (i+1) + ": ");
rainfall[i] = scan.nextDouble();
totalRainfall += rainfall[i];
if (i == 0)
{
max = min = rainfall[i];
}
{
if (rainfall[i] > max) {
max = rainfall[i];
maxQuarter = i + 1;
}
if (rainfall[i] < min) {
min = rainfall[i];
minQuarter = i + 1;
}
}
}
System.out.println("Total rainfall = "+totalRainfall);
System.out.println("Average rainfall = "+(totalRainfall / 4.0));
System.out.println("Max quarter rainfall = "+ max);
System.out.println("Min quarter rainfall = " + min);
System.out.println("Max quarter rainfall = "+ maxQuarter);
System.out.println("Min quarter rainfall = " + minQuarter);
}//end main
}//end class
Upon compiling, I get this exception when I try to put data in for quarter 5.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Test2OF.main(Test2OF.java:22)
I'm also pretty clueless as to how I can specify what year had the most/least amount of rainfall at the end.
Your help is appreciated! Thank you very much.
problem:
year*4
Your rainfall is only of a size 4 which means it is only good for 1 year which has 4 quarter.
solution:
You need to calculate the number of quarter of the specified number of years and then resize your array by using year*4
sample:
Scanner scan = new Scanner(System.in);
double[] rainfall;
double totalRainfall = 0.0;
double max = 0, min = 0;
int year = 0;
int maxQuarter = 1;
int minQuarter = 1;
System.out.println("Enter the number of years: ");
year = scan.nextInt();
rainfall = new double[year*4]
EDIT:
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double[] rainfall;
int curyear = 0;
double totalRainfall = 0.0;
double max = 0, min = 0;
int year = 0;
int maxQuarter = 1;
int minQuarter = 1;
// Prompt user for the number of years
System.out.println("Enter the number of years: ");
year = scan.nextInt();
rainfall = new double[year*4];
for (int i=0; i < year*4 + 1; i++)
{
if((i % 4) == 0 && i != 0)
{
System.out.println();
System.out.println("YEAR: " + ++curyear );
System.out.println("Total rainfall = "+totalRainfall);
System.out.println("Average rainfall = "+(totalRainfall / 4.0));
System.out.println("Max quarter rainfall = "+ max);
System.out.println("Min quarter rainfall = " + min);
System.out.println("Max quarter rainfall = "+ maxQuarter);
System.out.println("Min quarter rainfall = " + minQuarter);
System.out.println();
if(i == (year*4))
break;
}
System.out.print("Enter rainfall for quarter " + (i+1) + ": ");
rainfall[i] = scan.nextDouble();
totalRainfall += rainfall[i];
if (i == 0)
{
max = min = rainfall[i];
}
{
if (rainfall[i] > max) {
max = rainfall[i];
maxQuarter = i + 1;
}
if (rainfall[i] < min) {
min = rainfall[i];
minQuarter = i + 1;
}
}
}
}//end main
new double[4]; this is wrong.
if you get size from user then why you initialized?
give size after user provide size like new double[year];
First thing I notice right off of the bat is
Int year = Scanner.nextInt();
Blahblahblah
year = Scanner.nextInt();
Don't give year a value yet.
Int year;
Year = scanner.nextInt();
As for the rest I don't know if I want to help you on this. It is unethical to assist you because the point of this is to learn. Note that it is perfectly fine to tell you professor that you were unable to complete the assignment and that he would be of better assistance than the internet.
Sent from iPad so please excuse spelling and capitalization mistakes
public static void main(String[] args) {
int sum = 0;
int inputNum;
int counter;
float average;
double Max = 0;
double Min = 101;
Scanner NumScanner = new Scanner(System.in);
Scanner charScanner = new Scanner(System.in);
System.out.println("Enter the total number of exams you want a average");
counter = NumScanner.nextInt();
System.out.println("Please enter " + counter + " numbers:");
for(int i = 1; i<=counter ;i++){
inputNum = NumScanner.nextInt();
sum = sum + inputNum;
System.out.println();
if(inputNum > Max){
Max = inputNum;
}
if(inputNum < Min){
Min = inputNum;
}
if(inputNum > -1 && inputNum < 101){
sum = sum + inputNum;
}
else{
System.out.println("You entered a number that wasn't in the range of 0 to 100");
average = sum / counter;
}
}
}
}
Write a program using a loop that takes 10 values from a user representing exam grades (between 0 and 100) from the keyboard and outputs the minimum value, maximum value and average value of all the values entered. Your program should not accept values less than 0 or greater than 100. Problems with calculation of average and the program does not print out max and min values
How can I do this?
//This code is how to print out the max and min values of a list of numbers from the above program//
// Print out of max and min exam grades//
System.out.println( "Max Exam Score = " + Max );
System.out.println( "Minimum Exam Score = " + Min `enter code here`);
does anyone knows how to calculate the average in a loop. Every time I calculated the average I received 0 or 1. I know that I need use average = (sum) / (salary_annually); but I can't get it to work. Thanks in advance.
import java.util.Scanner;
public class midterm
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
int average=0;
int count = 0;
int salary_annually = 0;
for(int employee =1; employee <= 2; employee++)
{
System.out.println("Employee: " + employee);
for(int year=1; year <= 2; year++)
{
System.out.println("Please Enter the Salary for Year: " + year);
salary_annually = kb.nextInt();
sum += salary_annually ;
if (min >= salary_annually)
{
min = salary_annually;
}
if (max <=salary_annually)
{
max = salary_annually;
}
average = (sum) / (salary_annually);
}
System.out.println("The average is " + average);
System.out.println("The higher number " + max);
System.out.println("The the lowest number " + min);
}
}
}
I'm guessing the problem here is that you are using integer division. Since the sum and salary_annually are both integers division works slightly different. There is not remainder because dividing two integers gives an int.
For example 1/2 is not .5 as you might expect but instead it is 0. Any fractional number is dropped when doing integer math. As another example 9/5 is not 1.8 but instead 1.
If you want the average then you can either declare sum or salary_annually as a double and declare the average as a double as well.
Change
average = (sum) / (salary_annually);
To
double average=0;// Declare it as `double` rather than `int`
average = (sum) / 2.0;
average is calculated by: average = sum / count;
you need to increment your count variable, otherwise you will always get and ArithmeticException / by zero
The Average is calculated by average = sum / count where average should be of type double.
You did declare the variable count, but didn't use it.
import java.util.Scanner;
public class Calulate {
/**
* #param args
*/
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
double average = 0;
int count = 2;
int salary_annually = 0;
for (int employee = 1; employee <= 2; employee++) {
System.out.println("Employee: " + employee);
for (int year = 1; year <= count; year++) {
System.out.println("Please Enter the Salary for Year: " + year);
salary_annually = kb.nextInt();
sum += salary_annually;
if (min >= salary_annually) {
min = salary_annually;
}
if (max <= salary_annually) {
max = salary_annually;
}
}
average = sum / count;
System.out.println("The average is " + average);
System.out.println("The higher number " + max);
System.out.println("The the lowest number " + min);
}
}
}