JAVA第十八次上机作业

1.编写一个随机生成 10个 0(包括) 到 100 之间的随机正整数。

2.自学Date类和SimpleDateFormat类,用以下格式输出

系统当前时间公元2020年05月28日:今天是2020年的第149天,星期四

3.输入一个邮箱地址,判断是否合法.如果合法,输出用户名.

1.public class Test {

  public static void main(String[] args) {

  String str = "";

  for(int i = 1 ;i < 11 ;i++){

str=str+""+Math.round(Math.random()*100);

  if(i % 5 == 0 ){

  System.out.println(str);

  str = "";

  }

  }

  }

  }

2.import java.util.Date; 

import java.text.SimpleDateFormat;

public class NowString { 

   public static void main(String[] args) {      

   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

      System.out.println(df.format(new Date()));

   } 

3.public static boolean mailtest() {

        System.out.println("请输入你的邮箱地址:");

        Scanner sc = new Scanner(System.in);

        String s = sc.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.lastIndexOf("@") > s.lastIndexOf(".")) {

            System.out.println("输入的邮箱地址中.出现在了@前面");

            return false;

        }

        for (int i = 0; i < s.indexOf("@"); i++) {

            if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z' 

                    || s.charAt(i) >= 'A'&& s.charAt(i) <= 'Z'

                    ||s.charAt(i) >= '0'&& s.charAt(i) <= '9') {

                return true;

            } else {

                System.out.println("邮箱中包含了其他字符");

                return false;

            }

        }

        return true;

    }

}

 

posted @ 2020-05-28 11:23  杨博文  阅读(135)  评论(0)    收藏  举报