判断中间数字位置即左右相加的和相等,判断字符串长度

public class TestNumber {
static int x;
public static void main(String[] args) {
/*
*判断值6位置,左右相加的和相等
*/
String z = “1->2->3->4->5”;//5->4->3->2->1
StringBuffer sb = new StringBuffer(z).reverse();
String ss = sb.toString().replaceAll(">-", “->”);
System.out.println("ss = " + ss);

    int[] ints = {1, 2, 3, 4, 6, 4, 3, 2, 1};
    int sum1 = 0, sum2 = 0;
    for (int i = 0, j = ints.length - 1; j > 0; i++, j--) {
        if ((sum1 += ints[i]) == (sum2 += ints[j])) {
            x = (i + j) / 2;
        } else {
            return;
        }
    }
    System.out.println("值在第" + (x + 1) + "位");

    /*
     *判断String字符串长度
     * */
    String s1 = "12345";
    String s2 = "12345xx";
    System.out.println("s2 = " + new TestNumber().getLength(s1, s2));
}

public String getLength(String s1, String s2) {
    char[] c1 = s1.toCharArray();
    char[] c2 = s2.toCharArray();
    if (c2.length > c1.length) {
        return s2;
    }
    return s1;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值