Advertisement
cgorrillaha

Untitled

Nov 16th, 2021
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. package loops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MinMax {
  6.     public static void main(String[] args) {
  7.  
  8.     //variables - What do we need to track? What types?
  9.     int minTemp=Integer.MAX_VALUE, minTime=0;
  10.     int maxTemp=Integer.MIN_VALUE, maxTime=0;
  11.     int temp;//read in the temps here
  12.     Scanner s=new Scanner(System.in);
  13.     //inputs - read in the temperature each hour for 24 hours
  14.     /* computation is with inputs!  Check the current temp
  15.     * against the max and min temps
  16.     * if(temp>max)assign the temp to a variable for max
  17.     * reord the time the max was assigned
  18.     * if(temp<min)assign the temp to a variable for min
  19.     * record the time the min was assigned
  20.     * */
  21.     for(int time=0; time<24; time++){
  22.  
  23.         //promt and read in temp
  24.         System.out.println("Enter a temperature");
  25.         temp=s.nextInt();
  26.         //check temp against min
  27.         if(temp<minTemp){
  28.             minTemp=temp;
  29.             minTime=time;
  30.         }
  31.         //check temp against max
  32.         if(temp>maxTemp){
  33.             maxTemp=temp;
  34.             maxTime=time;
  35.         }
  36.     }
  37.  
  38.     //outputs
  39.         System.out.printf("Max temp was %d at %d:00%n", maxTemp, maxTime);
  40.         System.out.printf("Min temp was %d at %d:00%n", minTemp, minTime);
  41.     }
  42.  
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement