方法的概念

class JavaObject {
    public static void main(String [] args) {
        int sum = add(10,20);                    //第一步:赋值调用,调用了add方法,并传入了实际参数10,20分别传给形式参数,将sum赋值给s
        System.out.println("sum = " + sum);        //第五步:上下两个方法中的sum非同一个sum,不能互相访问     
        //add(30,40);        //单独调用不推荐。
        //System.out.println(add(30,40));   //输出不推荐
        int sum2 = add(30,40);
        System.out.println("sum2 = " + sum2);
    }
    //返回值类型int;明确参数列表int a,int b
    public static int add(int a,int b) {        //第二步:将10和20,分别传给a,b
        //int sum = a + b;                        //第三步:执行语句:将a和b相加后的结果,赋值给sum
        //return sum;                            //第四步:由return将sum返回给调用者
        return a + b;
    } 
}

import java.util.Scanner;        //键盘录入
class JavaObject {
    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        int max = getMax(x,y);
    }
}

方法重载

class JavaObject {
    public static void main(String [] args) {
        boolean b1 = isEquals(10,10);
        System.out.println(b1);
    }
    public static boolean isEquals(int a,int b) {
    return a == b;
    }
    public static boolean isEquals(double a,double b) {
    return a == b;
    }
}

面向对象类

  • 属性:姓名,年龄,性别
  • 行为:学习,睡觉
class JavaObject {
    public static void main(String [] args) {
        //类名 对象名 = new 类名();
        Student s = new Student();
        //调用属性并赋值
        s.name = "张三";
        s.age = 23;
        s.gender = "男";
        System.out.println(s.name + "..." + s.age + "..." + s.gender);   //使用变量
        //调用方法
        s.study();
        s.sleep();
    }
}
class Student {
    String name;        //姓名
    int age;            //年龄属性
    String gender;        //性别属性
    public void study() {   //定义学习的方法;
        System.out.println("学生学习");
    }
    public void sleep() {
        System.out.println("学生睡觉");
    }
}

方法的形式参数是类名的时候如何调用

class JavaObject {
    public static void main(String [] args) {
        print(10);   //赋值
        Student s = new Student();  //创建对象
        print(s);

    }
    public static void print(int x) {  //形式参数是基本数据类型,传递的值
        System.out.println(x);
    }
    //形式参数是引用数据类型,传递的地址值
    public static void print(Student stu) {  //Student stu = new Student();
        stu.name = "张三";
        stu.age = 23;
        stu.speak();
    }
}
class Student {
    String name;                //成员变量
    int age;
    public void speak() {  //局部变量
        System.out.println(name + "..." + age);            
    }
}   

private和this关键字

{
            Person p = new Person();
            p.setName("李四");
            p.setAge(32);
            p.speak();
}
class Person {
    private String name;
    private int age;
    public void setName(String name) {
        this.name = name;//在这里this代表p1的地址值
    }
    public String getName() {
        return name;
    }
    public void setAge(int age) {  //a = 17
        if (age > 18 && age<120) {
            this.age = age;
        } else {
            System.out.println("输入有误");
        }
    }
    public int getAge() {
        return age;
    }

    public void speak() {
        System.out.println(name + "..." + age);
    }
}  

Constructor构造方法

public Person() {                            //空参构造
}
public Person(String name,int age) {        //有参构造
    this.name = name;
    this.age = age;
}

static关键字


class Person {
    String name;
    static String country;
    public void speak() {  //说话的方法
        System.out.println(name + "..." + country);
    }
}
class JavaObject {    
    public static void main(String [] args) {
        Person.country = "日本";  //静态多了一种调用方式,可以通过类名。
            System.out.println(Person.country);

        Person e = new Person();  //创建对象
            e.name = "张老师";    //调用姓名属性并赋值
            e.country = "中国"; //调用国籍属性并赋值
            e.speak();
        Person f = new Person();  //创建对象
            f.name = "李老师";    //调用姓名属性并赋值
            f.speak();
    }
}

数组经典案例

class ArrayTool {
    public static int getMax(int[] arr) {
        int max = arr[0];   //记录第一个元素
        for (int i= 1 ;i < arr.length ;i++) {//从第二个元素开始遍历
            if (max < arr[i]) {//max与数组中其他的元素比较
                max = arr[i];//记录较大的
            }
        }
        return max;//将最大值返回
    }
    public static void print(int[] arr) {
        for (int i = 0;i < arr.length ; i++ ) { // 遍历数组
            System.out.print(arr[i] + " ");
        }
    }
    public static void revArray(int[] arr) {
        for (int i = 0; i < arr.length / 2; i++) { //循环次数是元素个数的一半
            //arr[0]与arr[arr.length-1-0]交换
            int temp = arr[i];
            arr[i] = arr[arr.length-1-i];
            arr[arr.length-1-i] = temp;
        }
    }
}

class Demo_ArrayTool {
    public static void main(String[] args) {
        int[] arr = {11,66,22,33,44,55,};
        int[] arr2 = {77,66,88,99,44,55,};
        
        //非静态的调用方法
        ArrayTool at = new ArrayTool();

        int max = at.getMax(arr);
        System.out.println(max + "最大");

        at.print(arr);
        System.out.println();

        at.revArray(arr2);
        at.print(arr2);

        //静态的调用方法
        //ArrayTool.getMax(arr);
    }
}

代码块

class JavaObject {
    static {
        System.out.println("JavaObject静态代码块");
    }
    
    public static void main(String[] args) {
        System.out.println("我是main方法");
        
        Student s1 = new Student();
        Student s2 = new Student();
    }
}


class Student {
    static {
        System.out.println("Student 静态代码块");
    }
    
    {
        System.out.println("Student 构造代码块");
    }
    
    public Student() {
        System.out.println("Student 构造方法");
    }
}

继承和this和super与构造方法的关系

class JavaObject {    
    public static void main(String[] args) {
        Son s = new Son();
        System.out.println(s.getName() + "..." + s.getAge());
        System.out.println("---------------------------");
        Son s2 = new Son("张三",23);
        System.out.println(s2.getName() + "..." + s2.getAge());
    }
}
class Father {
    private String name;
    private int age;

    /*public Father() {        //空参构造
        System.out.println("Father空参构造");
    }*/
    public Father(String name,int age) {
        this.name = name;
        this.age = age;
        System.out.println("Father有参构造");
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }
}
class Son extends Father {
    public Son() {
        //super("李四",24);//调父类
        this("王五",25);//调本类,不能同时写
        System.out.println("Son的空参构造");
    }
    public Son(String name,int age) {
        super(name,age);
        System.out.println("Son的有参构造");
    }
}

final

{
        final int num = 10;
        //num = 20;
        System.out.println(num);

        final Person p = new Person("张三",23);
        //p = new Person("李四",24);
        p.setName("李四");
        p.setAge(24);
        System.out.println(p.getName() + "..." + p.getAge());
        
        method(10);
        method(20);   
    public static void method(final int x) {
        System.out.pirntln(x);
    }
}

多态

class JavaObject {
    public static void main(String [] args) {
        Boy b = new Man();        //父类引用指向子类对象,人类
                                        
        System.out.println(b.name);    //父类引用指向子类对象就是向上转型
        b.eat();
        Man m = (Man)b;        //向下转型
        m.work();            //多肽的弊端,不能直接使用子类的

        //基本数据类型自动类型提升和强制类型转换
        int i = 10;
        byte c = 20;
        //i = c;        //自动类型提升
        c = (byte)i;   //强制类型转换

    }
}

class Boy {
    String name = "liming";
    public void eat() {
        System.out.println("吃一小碗饭");
    }
}
class Man extends Boy {
    String name = "laoli";
    public void eat () {
        System.out.println("吃一大碗饭");
    }
    public void work() {
        System.out.println("工作");
    }
}

案例

  • 需求说明:使用面向对象的思想,设计自定义类描述兔子和青蛙。实现思路及关键代码
分析兔子和青蛙的共性
根据共性,定义抽象的动物类
属性:名字、颜色、类别(哺乳类、非哺乳类)
方法:吃饭,发出叫声

根据青蛙会游泳 抽象游泳的接口
方法:游泳
定义兔子继承动物类,青蛙继承动物同时实现游泳接口

属性:name,color,类别classify
吃饭eat,声音voice
public abstatic Swim();游泳

public class JavaObject {
    public static void main(String [] args) {
        Frog f = new Frog("青蛙","绿色","不是");
        System.out.println(f.getName() + "是" + f.getColor() + "的,它" + f.getMammal() + "哺乳动物");
        f.eat();
        f.voice();
        f.swim();
        System.out.println("------------------------");
        Rabbit r = new Rabbit("兔子","白色","是");
        System.out.println(r.getName() + "是" + r.getColor() + "的,它" + r.getMammal() + "哺乳动物");
        r.eat();
        r.voice();
        r.swim();
    }
}

abstract class  Animal{
    private String name;
    private String color;
    private String mammal;
    public Animal() {
    }
    public Animal(String name,String color,String mammal) {
        this.name = name;
        this.color = color;
        this.mammal = mammal;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getColor() {
        return color;
    }
    public void setMammal(String mammal) {
        this.mammal = mammal;
    }
    public String getMammal() {
        return mammal;
    }
    public void eat() {
        System.out.println("它吃饭");
    }
    public void voice() {
        System.out.println("它会发出叫声");
    }
    public abstract void swim();//游泳
}
class Frog extends Animal {
    public Frog() {
    }
    public Frog(String name,String color,String mammal) {
        super(name,color,mammal);
    }
    public void swim() {
        System.out.println("它会游泳");
    }
}
class Rabbit extends Animal {
    public Rabbit() {
    }
    public Rabbit(String name,String color,String mammal) {
        super(name,color,mammal);
    }
    public void swim() {
        System.out.println("它不会游泳");
    }
}

编写程序描述影视歌三栖艺人

需求说明:请使用面向对象的思想,设计自定义类,描述影视歌三梄艺人。
分析影视歌三栖艺人的特性
可以演电影
可以演电视剧
可以唱歌
定义多个接口描述特性

演电影的接口-----方法:演电影
演电视剧的接口-----方法:演电视剧
唱歌的接口-----方法:唱歌
定义艺人类实现多个接口

public class JavaObject {
    public static void main(String [] args) {
        Art a = new Art("周杰伦",40);
        System.out.println(a.getName() + a.getAge() + "岁");
        a.movie();
        a.teleplay();
        a.sing();
    }
}

interface Movie{
    public abstract void movie();//演电影
}
interface Teleplay{
    public abstract void teleplay();//演电视剧
}
interface Sing{
    public abstract void sing();//唱歌
}

class Art implements Movie,Teleplay,Sing{
    private String name;
    private int age;
    public Art() {
    }
    public Art(String name,int age) {
        this.name = name;
        this.age = age;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }


    public void movie() {
        System.out.println("演电影");
    }
    public void teleplay() {
        System.out.println("演电视剧");
    }
    public void sing() {
        System.out.println("唱歌");
    }
}

案例

  • 需求说明编写程序,接收用户输入的信息,选择需要制作的比萨。
可供选择的比萨有:培根比萨和海鲜比萨。
实现思路及关键代码
分析培根比萨和海鲜比萨
定义比萨类
属性:名称、价格、大小
方法:展示
定义培根比萨和海鲜比萨继承自比萨类
定义比萨工厂类,根据输入信息产生具体的比萨对象
import java.util.Scanner;        //键盘录入
public class JavaObject {
    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入想要的披萨:“1”代表培根披萨,“2”代表海鲜披萨");
        int x = sc.nextInt();
        if (x == 1) {
            System.out.println("请输入想要的尺寸:");
            int y = sc.nextInt();
            Bacon b = new Bacon();
            b.setName("培根");
            b.setSize(y);
            System.out.print("您选择了" + b.getName() + "披萨,尺寸是" + b.getSize() + ",价格是" + 10 * b.getSize() + "元,");
            b.show();

        } else if (x == 2) {
            System.out.println("请输入想要的尺寸:");
            int y = sc.nextInt();
            Seafood s = new Seafood();
            s.setName("海鲜");
            s.setSize(y);
            System.out.print("您选择了" + s.getName() + "披萨,尺寸是" + s.getSize() + ",价格是" + 10 * s.getSize() + "元,");
            s.show();
        } else {
            System.out.println("输入错误,请重新输入。");
        }
    }
}

class Pizza {
    private String name;
    private int price;
    private int size;

    public Pizza() {
    }
    public Pizza(String name,int price,int size) {
        this.name = name;
        this.price = price;
        this.size = size;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public int getPrice() {
        return price;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public int getSize() {
        return size;
    }
}

class Bacon extends Pizza{
    public Bacon() {    
    }
    public Bacon(String name,int price,int size){
        super(name,price,size);
    }        
    public void show() {
        System.out.println("培根披萨是很好吃的,祝您用餐愉快");
    }
}
class Seafood extends Pizza{
    public Seafood() {    
    }
    public Seafood(String name,int price,int size){
        super(name,price,size);
    }        
    public void show() {
        System.out.println("海鲜披萨是很有特色的,祝您用餐愉快");
    }
}

案例

编写程序,接收用户输入的信息,选择购买的饮料。
可供选择的饮料有:咖啡、矿泉水和可乐。
其中,购买咖啡时可以选择:加糖、加奶还是什么都不加。
购买可乐时可以选择:买可口可乐还是百事可乐。

import java.util.Scanner;        //键盘录入
public class JavaObject {
    public static void main(String [] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入想要的饮料:1.咖啡、2.可乐、3.矿泉水");
        String x = input.next();
        Beverage b = Work.getB(x);
        b.show();
        }
}

class Beverage {
    private String name;

    public Beverage() {
    }
    public Beverage(String name) {
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void show() {
        System.out.println("您購買了" + name);
    }
}

class Coffee extends Beverage{
    private String soname;
    public Coffee() {    
    }
    public Coffee(String name,String soname){
        super(name);
        this.soname = soname;
    }
    public void setSoname(String soname) {
        this.soname = soname;
    }
    public String getSoname() {
        return soname;
    }    
    public void show() {
        System.out.println("您購買了" + super.getName());
        System.out.println("裡面加" + this.getSoname());
    }
}

class Work {
    public static Beverage getB(String no) {
        Scanner input = new Scanner(System.in);
        Beverage b = null;
        if ("1".equals(no)) {
            System.out.println("请问是否要配料(1、加糖 2、加奶 3、什么也不加)");
            int y = input.nextInt();
            String soname;
            switch (y) {
            case 1:
                soname = "加糖";
                break;
            case 2:
                soname = "加奶";
                break;
            default:
                soname = "什么一个不加";
            }
            b = new Coffee("咖啡", soname);
        } else if ("2".equals(no)) {
            System.out.println("请選擇品種(1、可口可樂 2、百事可樂)");
            int y = input.nextInt();
            String soname;
            switch (y) {
            case 1:
                soname = "可口";
                break;
            default:
                soname = "百事";
            }
            b = new Coffee("可樂", soname);
        } else if ("3".equals(no)) {
            b = new Water("礦泉水");
        } else {System.out.println("請重新輸入");}
        return b;
    }
}


class Coke extends Beverage{
    private String soname;
    public Coke() {    
    }
    public Coke(String name,String soname){
        super(name);
        this.soname = soname;
    }
    public void setSoname(String soname) {
        this.soname = soname;
    }
    public String getSoname() {
        return soname;
    }    

}

class Water extends Beverage{
    public Water() {    
    }
    public Water(String name){
        super(name);
    }
    public void water() {
        System.out.println("购买了矿泉水,谢谢惠顾");
    }
}


//第二種方法
import java.util.Scanner;        //键盘录入
public class JavaObject {
    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入想要的饮料:1.咖啡、2.可乐、3.矿泉水");
        int x = sc.nextInt();
        Beverage b = Work.getB(x);
        b.show();
        }
}

class Beverage {
    private String name;

    public Beverage() {
    }
    public Beverage(String name) {
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void show() {
        System.out.println("您購買了" + name);
    }
}

class Coffee extends Beverage{
    private String soname;
    public Coffee() {    
    }
    public Coffee(String name,String soname){
        super(name);
        this.soname = soname;
    }
    public void setSoname(String soname) {
        this.soname = soname;
    }
    public String getSoname() {
        return soname;
    }    
    public void show() {
        System.out.println("您購買了" + super.getName());
        System.out.println("裡面" + this.getSoname());
    }
}

class Coke extends Beverage{
    private String soname;
    public Coke() {    
    }
    public Coke(String name,String soname){
        super(name);
        this.soname = soname;
    }
    public void setSoname(String soname) {
        this.soname = soname;
    }
    public String getSoname() {
        return soname;
    }    
    public void show() {
        System.out.println("您購買了" + this.getSoname() + super.getName());
    }
}

class Water extends Beverage{
    public Water() {    
    }
    public Water(String name){
        super(name);
    }
    public void water() {
        System.out.println("购买了矿泉水,谢谢惠顾");
    }
}



class Work {
    public static Beverage getB(int no) {
        Scanner sc = new Scanner(System.in);
        Beverage b = null;
        if (no == 1) {
            System.out.println("请问是否要配料(1、加糖 2、加奶 3、什么也不加)");
            int y = sc.nextInt();
            String soname;
            switch (y) {
            case 1:
                soname = "加糖";
                break;
            case 2:
                soname = "加奶";
                break;
            default:
                soname = "什么也不加";
            }
            b = new Coffee("咖啡", soname);
        } else if (no == 2) {
            System.out.println("请選擇品種(1、可口可樂 2、百事可樂)");
            int y = sc.nextInt();
            String soname;
            switch (y) {
            case 1:
                soname = "可口";
                break;
            default:
                soname = "百事";
            }
            b = new Coke("可樂", soname);
        } else if (no == 3) {
            b = new Water("礦泉水");
        } else {System.out.println("請重新輸入");}
        return b;
    }
}

重点案例

  • 实现不同引用类型对象的大小比较
需求说明:学生类,新闻类,商品类虽然是完全不同的类,但是都具有比较的能力,比如可以比较两个学生的大小,但需要指定比较的依据是学号、姓名还是成绩等。

实现思路及关键代码:
将比较的能力定义为接口,让学生、新闻、商品类都实现该接口。
定义接口Comparable,其中包含唯一的方法int compareTo(Object obj);返回值>0,表示大于,返回值=0,表示等于,返回值<0,表示小于。
定义学生类,包括学号、姓名、年龄和分数,实现Comparable接口,按照分数倒序排列;
定义新闻类,包括编号(int类型)、标题、内容和点击数,实现Comparable接口,按照编号正序排列;
定义测试类,分别创建两个学生对象、新闻对象,进行比较并输出结果。

public class JavaObject {
    public static void main(String [] args) {
        Students[] sArr = JavaObject.inputS();
        JavaObject.sortS(sArr); //学生排序
        JavaObject.printS(sArr); //学生打印

        News[] nArr = JavaObject.inputN();
        JavaObject.sortN(nArr);    //新闻排序
        JavaObject.printN(nArr);    //新闻打印
    }

    //新闻打印
    public static void printN(News[] nArr) {
        System.out.println("--------------------------------");
        System.out.println("按新闻编号排序");
        System.out.println("新闻编号\t新闻内容");
        for (int i = 0; i < nArr.length; i++) {
            System.out.println(nArr[i].getId() + "\t" + nArr[i].getBody());
        }
    }

    //新闻排序
    public static void sortN(News[] nArr) {
        for (int i = 0; i < nArr.length - 1; i++) {
            for (int j = i + 1; j < nArr.length; j++) {
                if (nArr[i].compareTo(nArr[j]) > 0) {
                    News temp = nArr[i];
                    nArr[i] = nArr[j];
                    nArr[j] = temp;
                }
            }
        }
    }

    //录入新闻数组
    public static News[] inputN() {
        int id;
        String title;
        News[] nArr = new News[5];
        for (int i = 0; i < nArr.length; i++) {
            id = (int) (Math.random() * 11 + 10);// ID[10,20]
            
            title = "我的内容" + (i + 1);
            nArr[i] = new News(id, title);
        }
        return nArr;
    }

    //学生打印
    public static void printS(Students[] sArr) {
        System.out.println("--------------------------------");
        System.out.println("按学生成绩排序");
        System.out.println("学生姓名\t学生成绩");
        for (int i = 0; i < sArr.length; i++) {
            System.out.println(sArr[i].getName() + "\t" + sArr[i].getScore());
        }
    }

    //学生排序
    public static void sortS(Students[] sArr) {
        for (int i = 0; i < sArr.length - 1; i++) {
            for (int j = i + 1; j < sArr.length; j++) {
                if (sArr[i].compareTo(sArr[j]) > 0) {
                    Students temp = sArr[i];
                    sArr[i] = sArr[j];
                    sArr[j] = temp;
                }
            }
        }
    }

    //录入学生数组
    public static Students[] inputS() {
        int score;
        String name;
        Students[] sArr = new Students[5];
        for (int i = 0; i < sArr.length; i++) {
            name = "学生" + (i + 1);
            score = (int) (Math.random() * 12 + 80);
            sArr[i] = new Students(name, score);
        }
        return sArr;
    }
}

class Students implements Comparable{
    private String id;
    private String name;
    private int age;
    private int score;

    public Students() {
    }
    public Students(String name,int score) {
        this.name = name;
        this.score = score;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getId() {
        return id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public int getScore() {
        return score;
    }
    public int compareTo(Object obj) {
        Students s = (Students) obj;
        if (this.getScore() > s.getScore()) {
            return 1;
        } else if (this.getScore() < s.getScore()) {
            return -1;
        }
        return 0;
    }
}

class News implements Comparable{
    private int id;
    private String title;
    private String body;
    private int click;

    public News() {
    }
    public News(int id,String title) {
        this.id = id;
        this.title = title;
    }

    public void setId(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getTitle() {
        return title;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public String getBody() {
        return body;
    }
    public void setClick(int click) {
        this.click = click;
    }
    public int getClick() {
        return click;
    }
    public int compareTo(Object obj) {
        News n = (News) obj;
        if (this.getId() > n.getId()) {
            return 1;
        } else if (this.getId() < n.getId()) {
            return -1;
        }
        return 0;
    }
}

1.验证键盘输入的用户名不能为空,长度大于6,不能有数字。

  • 提示:使用字符串String类的相关方法完成
import java.util.Scanner;
public class Other_11_test {
    public static void main(String[] args) {
        login();
    }

    private static void login() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        for (int i = 0; i < 6; i++) {
            String s = sc.nextLine();
            if (checkNum(s)) {
                System.out.println("验证成功!");
                break;
            } else {
                if (i == 5) {
                    System.out.println("您的次数已到,请明天再来吧");
                    break;
                }
                System.out.println("您还有" + (5 - i) + "次机会,请重新输入。");
            }
        }
    }

    public static boolean checkNum(String str) {
        String msg = "";
        boolean flag = true;
        if (str.isEmpty()) {

            msg += "不能为空,";
            flag = false;
        }

        if (str.length() <= 6) {
            msg += "长度不能少于6,";
            flag = false;
        }

        if (getNum(str) && str.length() <= 6) {
            msg += "且";
            flag = false;
        }

        if (getNum(str)) {
            msg += "不能含有数字,";
            flag = false;
        }

        if (flag == false) {
            System.out.print(msg);
        }
        return flag;
    }

    private static boolean getNum(String s) {
        final String number = "0123456789";
        for (int j = 0; j < s.length(); j++) {
            if (number.indexOf(s.charAt(j)) != -1) {
                return true;
            }
        }
        return false;
    }

2.接收从键盘输入的字符串格式的年龄,分数和入学时间,转换为整数、浮点数、日期类型,并在控制台输出。

提示:使用包装类Integer、Double和日期转换类DateFormat实现

        Scanner sc1 = new Scanner(System.in);
        System.out.println("请输入你的年龄:");
        String s1 = sc1.nextLine();
        int i = Integer.parseInt(s1); 
        
        Scanner sc2 = new Scanner(System.in);
        System.out.println("请输入你的成绩:");
        String s2 = sc2.nextLine();
        double b = Double.parseDouble(s2); 
        
        Scanner sc3 = new Scanner(System.in);
        System.out.println("请输入入学时间:如2018.09.31");
        String s3 = sc3.nextLine();
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        Date d = sdf.parse(s3);                
        System.out.println("你的年龄是:" + i + "岁,成绩是:" + b + "分,入学时间是:" + sdf.format(d));

2.接收从键盘输入的字符串格式的年龄,分数和入学时间,转换为整数、浮点数、日期类型,并在控制台输出。
提示:使用包装类Integer、Double和日期转换类DateFormat实现

    Scanner sc1 = new Scanner(System.in);
    System.out.println("请输入你的年龄:");
    String s1 = sc1.nextLine();
    int i = Integer.parseInt(s1); 
    
    Scanner sc2 = new Scanner(System.in);
    System.out.println("请输入你的成绩:");
    String s2 = sc2.nextLine();
    double b = Double.parseDouble(s2); 
    
    Scanner sc3 = new Scanner(System.in);
    System.out.println("请输入入学时间:如2018.09.31");
    String s3 = sc3.nextLine();
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
    Date d = sdf.parse(s3);                
    System.out.println("你的年龄是:" + i + "岁,成绩是:" + b + "分,入学时间是:" + sdf.format(d));

 

1.生成10个[10,23)之间的随机整数

提示:分别使用Math.random()和Random类的nextDouble()或nextInt()实现

    private static void sum3() {
        Random r = new Random();
        for (int i = 0; i < 9; i++) {
            System.out.println(Math.floor(r.nextDouble() * 11 + 12));
        }
    }

    private static void sum2() {
        Random r = new Random();
        for (int i = 0; i < 9; i++) {
            System.out.println(r.nextInt(11) + 12);
        }
    }

    private static void sum1() {
        for (int i = 0; i < 9; i++) {
            int number = (int)(Math.random()*11 + 12);
            System.out.println(number);
        }
    }

扎瓦
36 声望33 粉丝

笑吾庐,门掩草,径封苔。