Java exercise

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ersatz;
 
import java.io.FileNotFoundException;
 
public class Ersatz {
  public static void main(String[] args) throws FileNotFoundException {
    try {
      register("bb", "123321", "b@b.b");
      System.out.println("success");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
 
  public static void register(String name, String password, String email) {
    if (!(name != null && password != null && email != null)) {
      throw new NullPointerException("argument cannot be null");
    }
    if (!(name.length() >= 2 && name.length() <= 4)) {
      throw new RuntimeException(String.format("%s invalid", name.length()));
    }
 
    if (!(password.length() == 6 && isDigital(password))) {
      throw new RuntimeException(String.format("%s invalid", password));
    }
 
    int i = email.indexOf("@");
    int j = email.indexOf(".");
    if (!(i > 0 && i < j)) {
      throw new RuntimeException(String.format("%s invalid", email));
    }
  }
 
  public static boolean isDigital(String s) {
    char[] chars = s.toCharArray();
    for (char value : chars) {
      if (!(value >= '0' && value <= '9')) {
        return false;
      }
    }
    return true;
  }
}

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.gibe;
 
public class Ersatz {
  public static void main(String[] args) {
    String s = "abcdefg";
    try {
      s = reverse(s, 0, 3);
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
    System.out.println(s);
  }
 
  public static String reverse(String s, int commenceIndex, int closureIndex) {
    if (!(s != null && commenceIndex >= 0 && closureIndex < s.length() && commenceIndex <= closureIndex)) {
      throw new RuntimeException("arguments wrong !");
    }
 
    char[] chars = s.toCharArray();
    char c;
    for (int i = commenceIndex, j = closureIndex; i < j; i++, --j) {
      c = chars[i];
      chars[i] = chars[j];
      chars[j] = c;
    }
    return new String(chars);
    //    while (commenceIndex < closureIndex) {
//      c = chars[commenceIndex];
//      chars[commenceIndex] = chars[closureIndex];
//      chars[closureIndex] = c;
//      commenceIndex++;
//      closureIndex--;
//    }
//    return String.valueOf(chars);
  }
 
  public static String reverse(String s) {
    return reverse(s, 0, s.length() - 1);
  }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.gibe;
 
public class Ersatz {
  public static void main(String[] args) {
    String name = "Willian Jefferson Clinton";
    print(name);
  }
 
  public static void print(String s) {
    if (s == null) {
      System.out.println("null input");
      return;
    }
    String[] strs = s.split(" ");
    if (strs.length != 3) {
      System.out.printf("invalid %s",s);
      return;
    }
    String format = String.format("%s, %s, .%c", strs[2], strs[0], strs[1].toUpperCase().charAt(0));
    System.out.println(format);
  }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.gibe;
 
public class Ersatz {
  public static void main(String[] args) {
    String str = "abcHsp U 1234";
    count(str);
  }
 
  public static void count(String s) {
    if (s == null) {
      System.out.println("null input");
      return;
    }
    int nums = 0;
    int lowers = 0;
    int uppers = 0;
    int others = 0;
    for (int i = 0; i < s.length(); ++i) {
      if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
        nums++;
      } else if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
        lowers++;
      } else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
        uppers++;
      } else {
        others++;
      }
    }
    System.out.println("nums : " + nums);
    System.out.println("lowers : " + lowers);
    System.out.println("uppers : " + uppers);
    System.out.println("others : " + others);
  }
}

  

 

posted @   ascertain  阅读(38)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-07-15 fabonacci sequence
点击右上角即可分享
微信分享提示