Java练习题 - 工具类

  • 编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。
public class Exercise_09_01 {
  public static void main(String[] args) {
      //读取命令行数据,并进行类型转换
      int x = Integer.parseInt(args[0]);
      int y = Integer.parseInt(args[1]);
      int z = x+y;
      System.out.println(z);
      
  }
}
  • 编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e”出现的次数。
public class Test {

  /**
   * @param args
   */
  public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      String s = in.next();
      int num = 0;
      for(int  i =0;i < s.length();i++){
          if(s.charAt(i)=='e'){
              num++;
          }
      }
      System.out.println(num);
  }

}
  • 生成十个0~100之间的随机数,放到数组中,然后排序输出。
public class Test {
  public static void num(){
      
      int[] a = new int[8];
      for(int i = 0;i
  • 巴黎时间比北京时间晚7个小时,纽约时间比北京时间晚12个小时,试编写一程序,根据输入的北京时间输出相应的巴黎和纽约时间。
import java.util.Calendar;

public class TimeTest {
  
  public void getTime(int y,int m,int d,int h,int mi){
      //获取Calendar实例
      Calendar c = Calendar.getInstance();
      //设置巴黎时间
      c.set(y,m,d,h-7,mi);
      
      //输出巴黎时间
      System.out.println("巴黎时间是"+c.get(Calendar.YEAR)+"年"+
                 ((c.get(Calendar.MONTH))+1)+"月"+
                 c.get(Calendar.DAY_OF_MONTH)+"日"+
                 c.get(Calendar.HOUR_OF_DAY)+"点"+
                 c.get(Calendar.MINUTE)+"分");
      //设置纽约时间
      c.set(y,m,d,h-12,mi);
      //输出纽约时间
      System.out.println("纽约时间是"+c.get(Calendar.YEAR)+"年"+
                 ((c.get(Calendar.MONTH))+1)+"月"+
                 c.get(Calendar.DAY_OF_MONTH)+"日"+
                 c.get(Calendar.HOUR_OF_DAY)+"点"+
                 c.get(Calendar.MINUTE)+"分");
      
  }

  public static void main(String[] args) {
      TimeTest ex = new TimeTest();
      //设置北京时间,月份是从0开始,数字2代表3月份
      ex.getTime(2015, 2, 28, 16, 50);
      
  }
}
  • 解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名.如果邮箱地址不合法则显示不合法的原因提示:
    邮箱地址不合法的因素:
    1)邮箱地址中不包含@或。
    2)邮箱地址中含有多了@或。
    3)邮箱地址中。出现在@的前面
    4)用户名里有其他字符
    实现步骤:
    (1) 创建一个类,类名:mailtest
    类图如下:
    [图片上传失败...(image-4980e0-1562067760735)]
    (类名和方法名必须与要求一样。区分大小写)
import java.util.Scanner;

public class Mailtest {
  public static boolean testMail(){
      Scanner in = new Scanner(System.in);
      String s = in.next();
      if(s.indexOf("@")==-1||s.indexOf(".")==-1){
          
          System.out.println("邮箱地址中不包含@或.");
          return false;
      }
      
      >if(s.indexOf("@")!=s.lastIndexOf("@")||s.indexOf(".")!=s.lastIndexOf(".")){
          System.out.println("邮箱地址中含有多了@或.");
          return  false;
      }
      
      if(s.indexOf("@")>s.lastIndexOf(".")){
          System.out.println("邮箱地址中.出现在@的前面");
          return false;
      }
      for(int i=0;i='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z') ){
              
          }else{
              System.out.println("用户名里有其他字符");
              return false;
          }
          
      }
      return true;
  }
}

public class Test {
  /**
   * @param args
   */
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      
      if(Mailtest.testMail()){
          System.out.println("邮箱格式合法");
      }else{
          System.out.println("邮箱格式不合法");
      }
  }
}
  • 分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。
import java.util.Scanner;

public class Test {
  /**
   * @param args
   */
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner in = new Scanner(System.in);
      String s = in.next();
      String a = in.next();
      
      int num = 0;//i是循環的次數 
      
      for(int i = 0;i
  • 有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。
import java.util.Scanner;

public class Test {

  /**
   * @param args
   */
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner in = new Scanner(System.in);
      String s = in.next();
      int num=0,eng=0,china=0;
      for(int i =0;i='0'&&s.charAt(i)<='9'){
              num++;
          }else if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')){
              eng++;
          }else if((19968 <= s.charAt(i) && s.charAt(i) <40623)) {
              china++;
          }

      }
      
      System.out.println("数字是:"+num);
      System.out.println("英文是:"+eng);
      System.out.println("中文是:"+china);
  }
}
  • 有一种数叫回文数,正读和反读都一样,如12321便是一个回文数。编写一个程序,从命令行得到一个整数,判断该数是不是回文数。
import java.util.Scanner;

public class Test {

  /**
   * @param args
   */
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      if(foo()){
          System.out.println("是回数");
      }else{
          System.out.println("不是回数");
      }
  }
  
  public static boolean foo(){
      Scanner in = new Scanner(System.in);
      String s = in.next();
      int j = s.length()-1;
      for(int i=0;i

你可能感兴趣的:(Java练习题 - 工具类)