diff --git "a/31 \346\235\216\346\254\243/20230513 \344\275\234\344\270\232.md" "b/31 \346\235\216\346\254\243/20230513 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..4f4318cf8e08eeac5caed8076e395881556807a6 --- /dev/null +++ "b/31 \346\235\216\346\254\243/20230513 \344\275\234\344\270\232.md" @@ -0,0 +1,39 @@ + 写一个求和的方法,getSum(int a,int b); +main方法中,让使用户输入两个整数,调用getSum,输出这两个数的和,因为用户可能坐输入不是整数的值,所以要处理异常 + +```java +import java.util.Scanner; + +public class Sum { + public static int getSum(int a,int b){ + return a+b; + } + public static void main(String[] args) { + while (true) { + Scanner sc = null; + try { + sc = new Scanner(System.in); + System.out.println("请输入第一个整数:"); + int a = sc.nextInt(); + while (true) { + try { + System.out.println("请输入第二个整数:"); + int b = sc.nextInt(); + System.out.println(getSum(a, b)); + break; + } catch (Exception e) { + System.out.println("输入有误,非整数范围"); + sc.next(); + } + } + break; + } catch (Exception e) { + System.out.println("输入有误,非整数范围"); + sc.next(); + } + } + } +} + +``` +