Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- My English is not very good, please forgive me!
- Changes:
- Code Structure
- 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.
- Separation of Concerns
- 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.
- Handling Exceptions
- 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.
- Input Validation
- 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.
- 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