You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
980 B

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import java.util.*;
  2. public class MathProblems
  3. {
  4. // Implement this method, which prompts the user for a and b, then divides a by b
  5. // and then calculates a % b. If the user enters an invalid type of input, or a
  6. // zero for the divisor, an Exception will be thrown, both of which need to be caught
  7. public void divide()
  8. {
  9. Scanner input = new Scanner(System.in);
  10. int a=1;
  11. int b=1;
  12. try
  13. {
  14. System.out.print("Enter a: ");
  15. a = input.nextInt();
  16. System.out.print("Enter b: ");
  17. b = input.nextInt();
  18. }
  19. catch(InputMismatchException ime)
  20. {
  21. System.out.println("Invalid input type");
  22. }
  23. catch(ArithmeticException ae)
  24. {
  25. System.out.println("You can't divide "+a+" by "+b);
  26. }
  27. }
  28. public static void main( String[] args )
  29. {
  30. // Test it here in main, when ready
  31. MathProblems functions = new MathProblems();
  32. functions.divide();
  33. }
  34. }