JAVA-2.1-上机

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

import java.util.Random;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Random r=new Random();
        for (int i = 0; i < 10; i++) {
            System.out.print(r.nextInt(101)+"\t");
        }
    }

}
2.通过电子版教材或者视频,自学Date类和SimpleDateFormat类,用以下格式输出
系统当前时间
公元2020年05月28日:今天是2020年的第149天,星期四
package practice;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    public static void main(String[] args) throws ParseException {
        // TODO Auto-generated method stub
        SimpleDateFormat d=new SimpleDateFormat("Gyyyy年MM月dd日:今天是yyyy年的第D天,E");
        Date s=new Date();
        String sc=d.format(s);
        System.out.println(sc);
    }

}
3.输入一个邮箱地址,判断是否合法.如果合法,输出用户名.
合法:必须包含@ 和 . 并且.在@的后面 (用indexof)
用户名: 例如  用户名为dandan (用subString)
package practice;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        show();
    }
    public static void show() {
        System.out.println("请输入邮箱编码:");
        Scanner input=new Scanner(System.in);
        String s=input.next();
        boolean result = s.isEmpty();
        if (true) {
            int c =s.indexOf("@");
            int b =s.indexOf(".");
            if (b==c+1) {
                System.out.println("输入合法!");
                String s1=s.substring(0, c);
                System.out.println(s1);
            }else {
                System.out.println("输入不合法!");
            }        
        }else {
            System.out.println("输入为空!");
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/yunlan/p/12983337.html