首页 > 其他 > 详细

定义两个变量 在不定义第三个变量的情况下交换两个变量的值

时间:2019-07-30 21:30:32      阅读:106      评论:0      收藏:0      [点我收藏+]

 

/**
 * 定义两个变量 在不定义第三个变量的情况下交换两个变量的值
 */
public class jiaoHuan {
 
    public static void main(String[] args) {
        String s1 = "00";
        String s2 = "abcd";
        System.out.println("交换之前 s1 = " + s1 + " s2 = " + s2);
        s2 = s1 + s2; //合并到s2
        System.out.println("s2 = " + s2); //00abcd
        s1 = s2.substring(s1.length());
        //截取前面长度为4的字符串 s1 = "abcd"
        s2 = s2.substring(0, s2.length() - s1.length());
        //截取最后长度为2的字符串 s2 = "00"
        System.out.println("交换后 s1 = " + s1 + " s2 = " + s2);
    }
 
 
}
 
/**
 * int类型
 */
class testInt {
 
    public static void main(String[] args) {
        int a = 1;
        int b = 9;
        System.out.println("交换之前 a = " + a + " ,b = " + b);
        a = a + b;
        b = a - b;
        a = a - b;
        System.out.println("交换之后 a = " + a + " ,b = " + b);
    }
}

 

定义两个变量 在不定义第三个变量的情况下交换两个变量的值

原文:https://www.cnblogs.com/mryangbo/p/11272865.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!