conCat()的应用

编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户;

package com.Summer_0511.cn;

import java.util.Scanner;

public class Test06 {
    public void conCat(String s1,String s2){
        String s = s1+s2;
        System.out.println(s);
    }
    
    public void conCat(int s1,int s2){
        int sum = s1+s2;
        System.out.println(sum);
    }
    
    public void conCat(double s1,double s2){
        double sum = s1 + s2;
        System.out.println(sum);
    }
    public static void main(String[] args) {
        //字符串拼接
        Test06 d = new Test06();
        Scanner sc = new Scanner(System.in);
        int n1 = 0,n2 = 0;
        double num1 = 0.0,num2 = 0.0;
        String s1,s2;
        System.out.println("输入两个字符串");
        s1 = sc.next();
        s2 = sc.next();
        try {
            n1 = Integer.parseInt(s1);
            n2 = Integer.parseInt(s2);
            num1 = Double.parseDouble(s1);
            num2 = Double.parseDouble(s2);
        } catch (NumberFormatException e) {
            System.out.println("输入不对,无法转换");
            e.printStackTrace();
        }
        
        d.conCat(s1, s2);
        d.conCat(n1, n2);
        d.conCat(num1, num2);
        
    }

}

 

posted @ 2019-05-11 23:08  Geek张东坡  阅读(273)  评论(0编辑  收藏  举报