アットウィキロゴ

ouchi > Practice > Enshu02-20

  1. import java.util.Scanner;
  2.  
  3. public class Driver {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner scan = new Scanner(System.in);
  8.  
  9. System.out.println("数字を入力してください");
  10. int x = scan.nextInt();
  11.  
  12. int doubleResult = 0;
  13.  
  14. try {
  15. doubleResult = toDouble(x);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. System.out.println("2倍した値: " + doubleResult);
  20.  
  21. int squareResult = toSquare(x);
  22. System.out.println("2乗した値: " + squareResult);
  23.  
  24. }
  25.  
  26. public static int toDouble(int x) throws Exception {
  27. if (x == 0) {
  28. throw new Exception("x = 0");
  29. }
  30. return x * 2;
  31. }
  32.  
  33. public static int toSquare(int x) {
  34. if (x == 0) {
  35. throw new RuntimeException("x = 0");
  36. }
  37. return x * x;
  38. }
  39.  
  40. }
  41.  
最終更新:2013年05月17日 12:02