Advertisement
SkullCN

Untitled

Jul 2nd, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. My English is not very good, please forgive me!
  2.  
  3. Changes:
  4. Code Structure
  5. The logic is spread out across multiple methods: getValidNumber, getValidOperator, and loop. This modular approach makes the code easier to understand and maintain because each method has a clear responsibility.
  6. Separation of Concerns
  7. The calculation of the result is separated into a distinct getResult method. This method takes the operator and two numbers as parameters and returns the result. This separation makes the code easier to understand and maintain, as each method has a clear responsibility.
  8. Handling Exceptions
  9. I've introduced an ExitException class to handle the case where the user types "exit." This approach uses exceptions to control the program's flow. If the user types "exit," the getValidNumber and getValidOperator methods throw this exception, which is then caught and handled in the main method. This makes the code even more complex, but I don't know how to simplify it.
  10. Input Validation
  11. Includes input validation in the getValidNumber and getValidOperator methods. If the user enters an invalid number or operator, the program displays an error message and waits for further input instead of throwing an exception.
  12. The regular expression [+\\-*/%] matches any string that is a single character and is one of "+", "-", "*", "/", or "%". This makes the code shorter and easier to read. I learned it from the internet, Mineacademy isn't teaching it at the moment.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement