Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- /*
- Amon Guinan
- Thursday, January 2019
- Program interprets User input temperature.
- Outputs predicted season.
- */
- public class Season_Finder2 {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.print("Input Valid Temperature (-5,110): ");
- String tempStr = input.nextLine();
- //Must be declared outside of try block
- int tempInt = 0;
- /*
- What if the user inputs a word or sentence?
- We need to return a pretty error message and not have the program stop without explaining the issue...
- */
- int x = 0;
- try{ //tries code that may fail without stopping program.
- tempInt = Integer.parseInt(tempStr);
- //Catches all NumberFormatExceptions but not other errors
- } catch(NumberFormatException e) {
- //Fix problem
- x = 1;
- }
- if(x == 1) // returns error message.
- {
- System.out.println("Input a number.");
- System.exit(2);
- }
- if((tempInt < (-5))||(tempInt > (110)))
- {
- System.out.print(tempInt+" degrees is outside valid range.");
- }
- if((tempInt >= (-5))&&(tempInt < (50)))
- {
- System.out.print("Winter. "+tempInt+" degrees.");
- }
- if((tempInt >= (50))&&(tempInt < (70)))
- {
- System.out.print("Fall. "+tempInt+" degrees.");
- }
- if((tempInt >= (70))&&(tempInt < (90)))
- {
- System.out.print("Spring. "+tempInt+" degrees.");
- }
- if((tempInt >= (90))&&(tempInt <= (110)))
- {
- System.out.print("Summer. "+tempInt+" degrees.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement