Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package loops;
- import java.util.Scanner;
- public class MinMax {
- public static void main(String[] args) {
- //variables - What do we need to track? What types?
- int minTemp=Integer.MAX_VALUE, minTime=0;
- int maxTemp=Integer.MIN_VALUE, maxTime=0;
- int temp;//read in the temps here
- Scanner s=new Scanner(System.in);
- //inputs - read in the temperature each hour for 24 hours
- /* computation is with inputs! Check the current temp
- * against the max and min temps
- * if(temp>max)assign the temp to a variable for max
- * reord the time the max was assigned
- * if(temp<min)assign the temp to a variable for min
- * record the time the min was assigned
- * */
- for(int time=0; time<24; time++){
- //promt and read in temp
- System.out.println("Enter a temperature");
- temp=s.nextInt();
- //check temp against min
- if(temp<minTemp){
- minTemp=temp;
- minTime=time;
- }
- //check temp against max
- if(temp>maxTemp){
- maxTemp=temp;
- maxTime=time;
- }
- }
- //outputs
- System.out.printf("Max temp was %d at %d:00%n", maxTemp, maxTime);
- System.out.printf("Min temp was %d at %d:00%n", minTemp, minTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement