Using an array and loops, write a program that will ask the user to enter a temp
ID: 3546849 • Letter: U
Question
Using an array and loops, write a program that will ask the user to enter a temperature for each day of the week and will find and print the average temperature, the low temperature, and the high temperature.
Please please help!
*This is a first year beginner java programming class (use putty) please keep it as basic and simple as possible. MUST use array and loops.
So far I have:
import java.util.*;
public class Lab9
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
//input temperature in a week
double[] week=new double[7];
for (int m=0;m<7;m++)
{
System.out.print("Day: "+m+1);
temp[m]=input.nextDouble();
}
//check height and lowest temperature
double high=0.0;
double low=10000.0;
for (int m=0; m<7; m++)
{
if (high<temp[m]){
high=temp[m];
}
if (low>temp[m])
{
low=temp[m];
}
Explanation / Answer
package mypro;
import java.util.*;
public class Lab9 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
//input temperature in a week
double[] temp=new double[7];
for (int m=0;m<7;m++){
System.out.print("Day : "+(m+1)+"=");
temp[m]=input.nextDouble();
}
//check height and lowest temperature
double high=temp[0];
double low=temp[0];
double avg=0.0;
for (int m=0; m<7; m++){
avg=avg+temp[m];
if (high>temp[m]){
high=temp[m];
}
if (low<temp[m])
{
low=temp[m];
}
//end of loop
}
System.out.println("The average temperature is="+String.format( "%.2f", avg/7 ));
System.out.println("The highest temperature is= "+high);
System.out.println("The lowest temperature is "+low);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.