NOT ABLE TO CORRECT THIS CODE..
import java.text.DecimalFormat;
public class PeopleWeights {
public static void main(String[] args) {
DecimalFormat decFor = new DecimalFormat("0.00");
int i = 0;
int n = 5;
double arr[]=new double[n];
for(i = 0; i < n; i++){
System.out.println("Enter weight " + (i+1)+": ");
}
System.out.print("\nYou entered: ");
for(i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
double total = 0;
double max = 0;
for(i=0; i<n; i++){
if(max < arr[i]){
max = arr[i];
}
total = total + arr[i];
}
DecimalFormat df = new DecimalFormat("#.##");
double average = total/n;
System.out.println("Total weight: "+ df.format(total));
System.out.println("Average weight: "+ df.format(average));
System.out.println("Max weight: "+ df.format(max));
return;
}
}
THIS ARE THE ERRORS I KEEP GETTING.
Compare output
0/1
Input 236
89.5
142
166.3
93
Your output starts with Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 0.0 0.0 0.0 0.0 0.0
Total weight: 0
Average
Expected output starts with Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 236.0 89.5 142.0 166.3 93.0
Compare output
0/1
Input 123.4
56
98
174
215.8
Your output starts with Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 0.0 0.0 0.0 0.0 0.0
Total weight: 0
Average
Expected output starts with Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 123.4 56.0 98.0 174.0 215.8
Compare output
0/1
Input 236
89.5
142
166.3
93
Your output starts with Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 0.0 0.0 0.0 0.0 0.0
Total weight: 0
Average weight: 0
Max weight: 0
Expected output starts with Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 236.0 89.5 142.0 166.3 93.0
Total weight: 726.8
Compare output
0/1
Input 236
89.5
142
166.3
93
Your output starts with Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 0.0 0.0 0.0 0.0 0.0
Total weight: 0
Average weight: 0
Max weight: 0
Expected output starts with Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 236.0 89.5 142.0 166.3 93.0
Total weight: 726.8
Average weight: 145.35999999999999
Compare output
0/1
Input 236
89.5
142
166.3
93
Your output Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 0.0 0.0 0.0 0.0 0.0
Total weight: 0
Average weight: 0
Max weight: 0
Expected output Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 236.0 89.5 142.0 166.3 93.0
Total weight: 726.8
Average weight: 145.35999999999999
Max weight: 236.0
Compare output
0/1
Input 123.4
56
98
174
215.8
Your output Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 0.0 0.0 0.0 0.0 0.0
Total weight: 0
Average weight: 0
Max weight: 0
Expected output Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Enter weight 5:
You entered: 123.4 56.0 98.0 174.0 215.8
Total weight: 667.2
Average weight: 133.44
Max weight: 215.8
You are not taking the input from the user. Use Scanner or BufferedReader to get what the user is entering at the console.
Read this question for more details:
How can I get the user input in Java?
You are not getting the output because you are not taking the input from the user. what you have to do is to get the input to a variable and add it to the array. To get the input from the console you can use Scanner.
Below is the edited code. Try this, I think you can get the output.
import java.text.DecimalFormat;
import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
DecimalFormat decFor = new DecimalFormat("0.00");
Scanner sc = new Scanner(System.in);
int i = 0;
int n = 5;
double arr[]=new double[n];
for(i = 0; i < n; i++){
System.out.println("Enter weight " + (i+1)+": ");
double weight = Double.parseDouble(sc.nextLine());
arr[i] = weight;
}
System.out.print("\nYou entered: ");
for(i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
double total = 0;
double max = 0;
for(i=0; i<n; i++){
if(max < arr[i]){
max = arr[i];
}
total = total + arr[i];
}
DecimalFormat df = new DecimalFormat("#.##");
double average = total/n;
System.out.println("Total weight: "+df.format(total));
System.out.println("Average weight: "+df.format(average));
System.out.println("Max weight: "+df.format(max));
return;
}
}
still getting a different error.Output 4, "Max weight:2".36 is highlighted with orange, Average weight: 145.3"6"- 6 is highlighted with orange EXPECTED OUTPUT Average weight: 145.3"5999999999999" the " " is highlighted. COMPARE OUTPUT 5: Your output Average weight: 145.3"6" - " " IS HIGHLIGHTED....expected output Average weight: 145.3"5999999999999" Max weight: 23"6.0" the " " are highlighted. Do you have a clue on how to fix it.
This is due to the use of DecimalFormat.
try:
import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = 0;
int n = 5;
double total = 0;
double max = 0;
double arr[]=new double[n];
for(i = 0; i < n; i++){
System.out.println("Enter weight " + (i+1)+": ");
double weight = Double.parseDouble(sc.nextLine());
arr[i] = weight;
if(max < arr[i]){
max = arr[i];
}
total = total + arr[i];
}
System.out.printf("%nYou entered: ");
for(i = 0; i < n; i++){
System.out.printf("%s ", arr[i]);
}
System.out.println();
double average = total/n;
System.out.printf("Total weight: %.1f%n", total);
System.out.println("Average weight: " + average);
System.out.printf("Max weight: %.1f%n" + max);
return;
}
}
This is what I used:
import java.util.Scanner;
/**
* Handles an array of peoples weights.
*/
public class PeopleWeights
{
// static variables that we can access in our static methods.
static final int WEIGHT_ARRAY_SIZE = 5;
static double[] weightsOfPeople = new double[WEIGHT_ARRAY_SIZE];
static double weightElement;
static double sum = 0.0;
static Scanner scnr = new Scanner(System.in);
public static void main(String[] args)
{
GetWeights();
WeightsSum();
WeightsAverage();
WeightsMax();
return;
}
/**
* Takes input for the weights and store them inside our array.
*/
public static void GetWeights()
{
int i = 0; // Local count variable.
// Take input for the weights and store them in our array.
for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
{
System.out.println("Enter weight " + (i + 1) + ": ");
weightElement = scnr.nextDouble();
weightsOfPeople[i] = weightElement;
}
// Display what the user entered
System.out.print("\nYou entered: ");
for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
{
System.out.print(weightsOfPeople[i] + " ");
}
return;
}
/**
* Gets the sum of our weights and then displays that sum.
*/
static void WeightsSum()
{
int i = 0;
for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
{
sum += weightsOfPeople[i];
}
System.out.println("\nTotal weight: " + sum);
return;
}
/**
* Gets the average of our weights and then displays that average.
*/
static void WeightsAverage()
{
double averageWeight = 0.0;
// Convert WEIGHT_ARRAY_SIZE to a double for good calculation.
averageWeight = sum / (double)WEIGHT_ARRAY_SIZE;
System.out.println("Average weight: " + averageWeight);
return;
}
/**
* Gets the max of our weights and then displays that max.
*/
static void WeightsMax()
{
int i= 0;
double maxWeight = 0.0;
for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
{
if(weightsOfPeople[i] > maxWeight)
{
maxWeight = weightsOfPeople[i];
}
}
System.out.println("Max weight: " + maxWeight);
return;
}
}
Related
import java.util.Scanner;
import java.text.DecimalFormat;
public class BasicOperator {
public static void main(String[] args) {
// this program will ask user for grades
//declare variables
DecimalFormat f = new DecimalFormat("#,##0.00");
double quiz1, quiz2, quiz3, quiz4, quiz5, sum1;
double hWork1, hWork2, hWork3, hWork4, hWork5, sum2;
double lab, fTest, extra, average1, average2, average3;
double letterGrade = 0;
//create scanner object
Scanner kb = new Scanner(System.in);
//get user quiz grade
System.out.println("Please enter your 1st quiz grade. ");
quiz1 = kb.nextDouble();
System.out.println("Please enter your 2nd quiz grade. ");
quiz2 = kb.nextDouble();
System.out.println("Please enter your 3rd quiz grade. ");
quiz3 = kb.nextDouble();
System.out.println("Please enter your 4th quiz grade. ");
quiz4 = kb.nextDouble();
System.out.println("Please enter your 5th quiz grade. ");
quiz5 = kb.nextDouble();
//get user homework grade
System.out.println("Please enter your 1st homework grade. ");
hWork1 = kb.nextDouble();
System.out.println("Please enter your 2nd homework grade. ");
hWork2 = kb.nextDouble();
System.out.println("Please enter your 3rd homework grade. ");
hWork3 = kb.nextDouble();
System.out.println("Please enter your 4th homework grade. ");
hWork4 = kb.nextDouble();
System.out.println("Please enter your 5th homework grade. ");
hWork5 = kb.nextDouble();
//get user lab exercise, final test, and extra credit grades
System.out.println("Please enter your lab grade. ");
lab = kb.nextDouble();
System.out.println("Please enter your final test grade. ");
fTest = kb.nextDouble();
System.out.println("Please enter your extra credits grade. ");
extra = kb.nextDouble();
//calculate the weighted average grade
sum1 = quiz1 + quiz2 + quiz3 + quiz4 + quiz5;
sum2 = hWork1 + hWork2 + hWork3 + hWork4 + hWork5;
average1 = lab;
average2 = fTest;
average3 = extra;
letterGrade = (sum1 / 5 * 0.15 + sum2 / 5 * 0.5 + average1 * 0.15 + average2 * 0.2 + average3 * 0.05);
//calculate the weighted average grade
sum1 = quiz1 + quiz2 + quiz3 + quiz4 + quiz5;
sum2 = hWork1 + hWork2 + hWork3 + hWork4 + hWork5;
average1 = lab;
average2 = fTest;
average3 = extra;
letterGrade = (sum1 / 5 * 0.15 + sum2 / 5 * 0.5 + average1 * 0.15 + average2 * 0.2 + average3 * 0.05);
//display the weighted average and letter grade
System.out.println("The weighted average is " + f.format(sum1 / 5 * 0.15 + sum2 / 5 * 0.5 + average1 * 0.15 + average2 * 0.2 + average3 * 0.05));
if(letterGrade >= 90 && letterGrade <= 100)
{
System.out.println("Your letter grade is A ");
}
else
if(letterGrade >= 80 && letterGrade <= 89 )
{
System.out.println("Your letter grade is B ");
}
else
if(letterGrade >= 70 && letterGrade <= 79)
{
System.out.println("Your grade is C ");
}
else
if(letterGrade >= 60 && letterGrade <= 69)
{
System.out.println("Your letter grade is D");
}
else
{
if(letterGrade < 60)
{
System.out.println("Your letter grade is F");
}
}
}
}
I've tried getting help from the tutors at my college. They kept saying the code is correct but do not know why the letter grade will not display in the console. What I am expecting is to see is Your letter grade is D after typing in 68 62 45 55 92 71 70 66 73 33 58 75 100. However, when I type in 78 75 90 85 68 90 92 84 78 93 75 81 100 I see The weighted average is 88.03 and Your letter grade is B.
public static void main (String [] args) {
//declare variables
int numbers;
int secondHighest;
// create scanner to enter 10 digits
Scanner scanner = new Scanner(System.in);
System.out.print("Enter 10 digits: ");
numbers = scanner.nextInt();
for (numbers = 0; numbers > 10; numbers++);
for (secondHighest =+ 0 ; secondHighest < 10; secondHighest++)
System.out.print( secondHighest);
this how far I reached, I can have the user enter those 10 digits but having an issue figuring out how to get the program to identify which of these 10 numbers is the second-highest also worth mentioning that the numbers need to be written in one line as such:
Provide 10 integers: 67 -468 36 1345 -7778 0 34 7654 45 -666
The second largest is: 1345
You can do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int num = 0, highest = Integer.MIN_VALUE, secondHighest = Integer.MIN_VALUE + 1;
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= 10; i++) {
System.out.print("Enter an integer: ");
num = scanner.nextInt();
if (num > secondHighest && num <= highest) {
secondHighest = num;
}
if (num > highest) {
secondHighest = highest;
highest = num;
}
}
System.out.println("Second highest number is: " + secondHighest);
}
}
A sample run:
Enter an integer: 10
Enter an integer: -2
Enter an integer: 0
Enter an integer: 34
Enter an integer: -78
Enter an integer: 78
Enter an integer: 56
Enter an integer: 0
Enter an integer: 89
Enter an integer: -34
Second highest number is: 78
[Update]
If you want to enter all the numbers in the same line, do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int num = 0, highest = Integer.MIN_VALUE, secondHighest = Integer.MIN_VALUE + 1;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter 10 integers: ");
for (int i = 1; i <= 10; i++) {
if (scanner.hasNextInt()) {
num = scanner.nextInt();
}
if (num > secondHighest && num <= highest) {
secondHighest = num;
}
if (num > highest) {
secondHighest = highest;
highest = num;
}
}
System.out.println("Second highest number is: " + secondHighest);
}
}
A sample run:
Enter 10 integers: 10 -3 24 0 56 -5 34 78 89 -6
Second highest number is: 78
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int highestNumber = 0;
int secondHighestNumber = 0;
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 10; i++){
System.out.println("Enter a digit: " + (i+1) + " of 10");
int enteredNumber = scanner.nextInt();
if (i == 0){
highestNumber = enteredNumber;
secondHighestNumber = enteredNumber;
} else if ((enteredNumber < highestNumber) && (enteredNumber > secondHighestNumber)){
secondHighestNumber = enteredNumber;
} else if (enteredNumber > highestNumber){
secondHighestNumber = highestNumber;
highestNumber = enteredNumber;
}
}
System.out.println(
"Highest number is: " + highestNumber + '\n' +
"Second highest number is: " + secondHighestNumber
);
}
}
# Arvind Kumar Avinash
Your code is fine and things, but there's a problem. If the first number is the least one and the second number is highest one It doesn't work pretty well. Your code gives an output then:
Enter an integer: -120
Enter an integer: 998
Enter an integer: 994
Enter an integer: 25
Enter an integer: 12
Enter an integer: 36
Enter an integer: 256
Enter an integer: 398
Enter an integer: 578
Enter an integer: 22
Second highest number is: -120
This program is supposed to take user input of four grades, take these grades and calculate the lowest, highest and average of them. Then it needs to print out the four grades along with the lowest, highest, and average of them with proper labels. I cannot figure out how to print out the four grades with my code, and for some reason it prints out the lowest, highest and average after every iteration of the loop, or every user input.
Here is what I have so far:
public class Test2 {
double total = 0.0;
double max = 0.0;
double min = Double.MAX_VALUE;
public void Test2 (double[] grades){
//Loop through all of the grades.
for(int i = 0; i < 4; i++){
double grade = grades[i];
//Add the grade to the total
total += grade;
//If this is the highest grade we've encountered, set as the max.
if(max < grade){
max = grade;
}
//If this is the lowest grade we've encountered, set as min.
if(min > grade){
min = grade;
}
}
System.out.println("Average is: " + (total / 4));
System.out.println("Max is: " + max);
System.out.println("Min is: " + min); }
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[] grades = new double[4];
System.out.println("Please enter number");
for (int i = 0; i < grades.length; i++) {
grades[i] = input.nextDouble();
Test2 g = new Test2();
g.Test2(grades);
} } }
Can anyone help me with this? I need it to print out the four grades (user input), along withe the lowest, highest and average grade from the four grades, but ONLY ONCE, not after every iteration of the loop. Sorry if my code looks bad.
You have to call the method Test2(double grade) only once in the main method as there is a for loop inside Test2 method. I.e call Test2 method in main outside for loop.
Your answer should be the below class.
import java.util.Scanner;
public class Test2 {
double total = 0.0;
double max = 0.0;
double min = Double.MAX_VALUE;
public void doOperations(double[] grades) {
for (int i = 0; i < 4; i++) {
double grade = grades[i];
//Add the grade to the total
total += grade;
//If this is the highest grade we've encountered, set as the max.
if (max < grade) {
max = grade;
}
//If this is the lowest grade we've encountered, set as min.
if (min > grade) {
min = grade;
}
}
System.out.println("Average is: " + (total / 4));
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[] grades = new double[4];
System.out.println("Please enter number");
for (int i = 0; i < grades.length; i++) {
grades[i] = input.nextDouble();
}
Test2 test2 = new Test2();
test2.doOperations(grades);
}
}
I'm very new to java and I'm wondering on how to stop the user from entering an input after the 10th. (inputs are separated by space)
sample output would be:
Please type 10 temperatures in Celsius: 30 12 20.5 ..
Temperatures in Celsius: 30.0 12.0 20.5 …
Temperatures in Fahrenheit: 86.0 53.6 68.9 …
Here's my code:
import java.util.Scanner;
public class array {
public static void main(String args[]) {
double [] a = new double [10];
Scanner sc=new Scanner(System.in);
System.out.print("Please type 10 temperatures in Celsius: ");
for(int j=0; j<10; j++)
a[j] = sc.nextDouble();
System.out.print("Temperatures in Celsius: ");
for (int i=0;i<a.length;i++)
System.out.print(a[i] + " ");
System.out.print("\nTemperatures in Fahrenheit: ");
for (int i = 0; i < a.length; i++)
a[i] = a[i]*1.8+32;
for (int i = 0; i < a.length; i++) {
System.out.printf("%.1f ", a[i]);
}
}
}
This is not possible, because input is provided from a terminal/shell, and this terminal usually reads input linewise. You have to do much more low level coding to handle scenarios like these.
Just output an error if the users enters more than 10 values and let him enter them again.
Daniel is correct, with your current setup its not quite possible. You could do it, if you're ok with the console looking a bit different. This requires more 'Enter' presses, and moves each temp to its own line.
Enter 10 temps:
Enter 1: [user input here]
Enter 2: [user input here]
etc etc
System.out.println("Enter 10 temps");
for(int i = 1; i <= 10; i++){
System.out.print("Enter " + i + ":");
a[i-1] = scanner.nextDouble();
}
And you can do some thing like this evry time put a number fo 10 times for each will print temperture in Celsius and in Fahrenheit
public static void main(String args[]) {
int counter = 0;
double[] a = new double[10];
Scanner sc = new Scanner(System.in);
System.out.print("Please type temperatures in Celsius: ");
while(counter < 10){
a[counter] = sc.nextDouble();
System.out.println("Temperatures in Celsius: " + a[counter]);
double tF = a[counter]*1.8 + 32;
System.out.println("Temperatures in Fahrenheit: " + tF);
counter++;
}
}
I am making a program for a java class of mine that asks this:
Write A Rainfall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following:
The total rainfall for the year
The average monthly rainfall
The month with most rain
The month with least rain
Demonstrate the class in a complete program. (Do not accept negative numbers for monthly rainfall figures)
import java.util.Scanner;
import java.io.*;
public class apples{
public static void main (String[] args){
Scanner kenny = new Scanner(System.in);
double rain[]=new double[13];
double sum = 0;
double avg =0;
double most =0;
double least =0;
System.out.println("Your local weather man here getting paid to tell you the wrong weather!!");
System.out.println("");
System.out.println("Please enter in the following rainfall for the months ahead: ");
System.out.println("Month\tRainfall (In inches)");
System.out.print("January: ");
rain [0] = kenny.nextDouble();
System.out.print("February: ");
rain [1] = kenny.nextDouble();
System.out.print("March: ");
rain [2] = kenny.nextDouble();
System.out.print("April: ");
rain [4] = kenny.nextDouble();
System.out.print("May: ");
rain [5] = kenny.nextDouble();
System.out.print("June: ");
rain [6] = kenny.nextDouble();
System.out.print("July: ");
rain [7] = kenny.nextDouble();
System.out.print("August: ");
rain [8] = kenny.nextDouble();
System.out.print("September: ");
rain [9] = kenny.nextDouble();
System.out.print("October: ");
rain [10] = kenny.nextDouble();
System.out.print("November: ");
rain [11] = kenny.nextDouble();
System.out.print("December: ");
rain [12] = kenny.nextDouble();
//(Or rain[] = 1,2,3,4,5,6,7,8,9,10,11,12);
sum = rain[0] + rain[1] + rain[2] + rain[3] + rain[4] + rain[5] + rain[6] + rain[6] + rain[7] + rain[8] + rain[9] + rain[10] + rain[11] + rain[12] ;
avg = (rain[0] + rain[1] + rain[2] + rain[3] + rain[4] + rain[5] + rain[6] + rain[6] + rain[7] + rain[8] + rain[9] + rain[10] + rain[11] + rain[12]) / 12;
System.out.println("The sum of all the rain is: " + sum);
System.out.println("The average rainfall was:" + avg + " inches");
System.out.print("The month with the most rain was: ");
}
private static void getMaxValue(double[] rain) {
getMaxValue(rain);
System.out.println(getMaxValue(rain));
System.out.println("The month with the least rain was: ");
}
private static void getMinValue(double[] rain) {
getMinValue(rain);
System.out.println(getMaxValue(rain));
}}
I've got most of it ready to go. I just am wondering how to get the "Max" and "Min" from the numbers that are entered.
Any help would be great!!
you can find max or min by looping array .and change return type void to double so method will return max rain ;
private static double getMaxValue(double[] rain) {
double max=0;
for(double i : rain){
if(i>max){
max=i;
}
}
return max;
}
and use this as;
System.out.println(getMaxValue(rain));
and same for min;
private static double getMinValue(double[] rain) {
double min=Double.MAX_VALUE;
for(double i : rain){
if(i<min){
min=i;
}
}
return min;
}
but in your code there is lot of mistakes
1)
double rain[]=new double[13];
this should be
double rain[]=new double[12];
because this is array length .so you have 12 months .
2) you have missed
rain [3]
3) you assign to 13 index by it should be 12 .
rain [13] = kenny.nextDouble(); --> rain [12] = kenny.nextDouble();
so this is the complete example .
public class apples {
public static void main(String[] args) {
Scanner kenny = new Scanner(System.in);
double rain[] = new double[12];
double sum = 0;
double avg = 0;
double most = 0;
double least = 0;
System.out.println("Your local weather man here getting paid to tell you the wrong weather!!");
System.out.println("");
System.out.println("Please enter in the following rainfall for the months ahead: ");
System.out.println("Month\tRainfall (In inches)");
System.out.print("January: ");
rain[0] = kenny.nextDouble();
System.out.print("February: ");
rain[1] = kenny.nextDouble();
System.out.print("March: ");
rain[2] = kenny.nextDouble();
System.out.print("April: ");
rain[3] = kenny.nextDouble();
System.out.print("May: ");
rain[4] = kenny.nextDouble();
System.out.print("June: ");
rain[5] = kenny.nextDouble();
System.out.print("July: ");
rain[6] = kenny.nextDouble();
System.out.print("August: ");
rain[7] = kenny.nextDouble();
System.out.print("September: ");
rain[8] = kenny.nextDouble();
System.out.print("October: ");
rain[9] = kenny.nextDouble();
System.out.print("November: ");
rain[10] = kenny.nextDouble();
System.out.print("December: ");
rain[11] = kenny.nextDouble();
//(Or rain[] = 1,2,3,4,5,6,7,8,9,10,11,12);
sum = rain[0] + rain[1] + rain[2] + rain[3] + rain[4] + rain[5] + rain[6] + rain[7] + rain[8] + rain[9] + rain[10] + rain[11];
avg = sum / 12;
System.out.println("The sum of all the rain is: " + sum);
System.out.println("The average rainfall was:" + avg + " inches");
most =getMaxValue(rain);
least=getMinValue(rain);
System.out.println("The max rain is: " + most);
System.out.println("The min rain is: " + least);
}
private static double getMaxValue(double[] rain) {
double max = 0;
for (double i : rain) {
if (i > max) {
max = i;
}
}
return max;
}
private static double getMinValue(double[] rain) {
double min = Double.MAX_VALUE;
for (double i : rain) {
System.out.println(i);
if (i < min) {
min = i;
}
}
System.out.println(min);
return min;
}
}
but you can use a array witch contain all the months.the advantage of this is you loop dynamically rather than hard cording .and you can warn when input negative easily .the good approach is follow .
public class apples {
public static void main(String[] args) {
Scanner kenny = new Scanner(System.in);
double rain[] = new double[12];
double sum = 0;
double avg = 0;
double most = 0;
double least = 0;
System.out.println("Your local weather man here getting paid to tell you the wrong weather!!");
System.out.println("");
System.out.println("Please enter in the following rainfall for the months ahead: ");
System.out.println("Month\tRainfall (In inches)");
String months[]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for (int i=0;i<months.length;i++) {
System.out.println(months[i]+" :");
double val = kenny.nextDouble();
while(val<0){
System.out.println("negatives not allowed ! enter again");
val = kenny.nextDouble();
}
rain[i]=val;
sum+=val;
}
avg = sum / 12;
System.out.println("The sum of all the rain is: " + sum);
System.out.println("The average rainfall was:" + avg + " inches");
most =getMaxValue(rain);
least=getMinValue(rain);
System.out.println("The max rain is: " + most);
System.out.println("The min rain is: " + least);
}
private static double getMaxValue(double[] rain) {
double max = 0;
for (double i : rain) {
if (i > max) {
max = i;
}
}
return max;
}
private static double getMinValue(double[] rain) {
double min = Double.MAX_VALUE;
for (double i : rain) {
System.out.println(i);
if (i < min) {
min = i;
}
}
System.out.println(min);
return min;
}
}
Java 8 has a much easier mechanism for manipulating data such as this without any need for looping or temporary variables.
If you have Java 8 you can use the following:
double rain[] = {3, 2, 7, 9, 10};
double totalRainfall = Arrays.stream(rain).sum;
double maxRainfall = Arrays.stream(rain).max().getAsDouble();
double minRainfall = Arrays.stream(rain).min().getAsDouble();
double avgRainfall = Arrays.stream(rain).average().getAsDouble();
That's much easier to read and understand than the traditional method.