2021-4-4 API

API

Scanner类

练习题

1.求两数之和

package com.wang.API.Scanner.Demo1.Demo2;

import java.util.Scanner;

public class Sum {
    public static void main(String[] args) {
        int c;
        Scanner scanner = new Scanner(System.in);

        int a = scanner.nextInt();
        int b = scanner.nextInt();
        c = a + b;
        System.out.println(c);
    }
}

2.求三数比大小:

package com.wang.API.Scanner.Demo1.Demo2;

import java.util.Scanner;

public class Max {
    public static void main(String[] args) {
        int max;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个数字:");
        int a = scanner.nextInt();
        System.out.println("请输入第二个数字:");
        int b = scanner.nextInt();
        System.out.println("请输入第三个数字:");
        int c = scanner.nextInt();
        if (a>b){
            if (a>c){
                max = a;
            }else{
                max = c;
            }
        }else{if (b>c){
            max = b;
        }else {
            max = c;
        }
        }
        System.out.println("最大值为:"+max);
    }
}

匿名对象

package com.wang.API.Scanner.Demo1.Demo3;

/*
匿名对象就是只有右边的对象,没有左边的名字和赋值运算符
new 类名称();

注意事项:匿名对象只能使用一次,下次使用又需要再次创建
 */

public class Demo3 {
    public static void main(String[] args) {
        Person one = new Person();
        one.name="高圆圆";
        one.showName();

        System.out.println("============");
        new Person().name = "赵又廷";
        new Person().showName();
        //出来的是我叫null
        //因为此对象又被new了一次
        
    }
}

用匿名对象使用Scanner

package com.wang.API.Scanner.Demo1.Demo3;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        //匿名对象 
        int num = new Scanner(System.in).nextInt();
        System.out.println("输入的是:"+num);
    }
}

Random

package com.wang.API.Scanner.Demo1.Demo4;

/*
Random类用来生成随机数字
1.导包
2.创建
Random r = new Random();
3,使用
获取一个随机的int数字:
int num = r.nextInt()  (此时生成的数的范围是int的范围)
int num = r.nextInt(n) (此时的范围是【0,n)
*/

import java.util.Random;

public class Demo1 {
    public static void main(String[] args) {
        Random random =new Random();
        int a = random.nextInt(100);
        System.out.println("随机数字为:"+a);
    }
}

使用Randmom创建的1-100之间猜数游戏练习

将随机值控制在1-100之间

将可以猜测的次数控制在10次之内

package com.wang.API.Scanner.Demo1.Demo4;

import java.util.Random;
import java.util.Scanner;

public class Game {
    public static void main(String[] args) {
        Random random = new Random();
        int a = random.nextInt(100) + 1;
        //变为1-100的范围
        int i = 0;
        int n = 0;

        System.out.println("请输入你猜的数字:");
        do {
            n++;

            int num = new Scanner(System.in).nextInt();
            if (num > a) {
                System.out.println("猜大了");
            } else if (num < a) {
                System.out.println("猜小了");
            } else {
                i++;
                System.out.println("恭喜你,猜对啦!");
                break;//猜中跳出循环
                }
            if (n>10){
                System.out.println("您猜测的次数超过十次");
                break;
            }else {
                System.out.println("请输入你再次猜测的数字:");
            }
        } while (i == 0);
        if (n <= 10){
            System.out.println("您猜对数字一共花费了"+n+"次。");
            System.out.println("游戏结束!");
        }else {
            System.out.println("游戏失败!");
        }

    }
}

对象数组

main方法

package com.wang.API.Array;

/*
题目:
要求定义一个数组,用来存储三个Person对象

数组有一个缺点
一旦创建,数组的长度不能发生改变了
 */

public class Demo1 {
    public static void main(String[] args) {
        Person[] array = new Person[3];
        Person one = new Person("迪丽热巴",18);
        Person two = new Person("古力娜扎",28);
        Person three = new Person("马尔扎哈",38);

        array[0] = one;//将one中的地址值 赋值到0号元素中
        array[1] = two;
        array[2] = three;

        System.out.println(array[1].getName());
    }
}

Person类

package com.wang.API.Array;

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

ArrayList

package com.wang.API.ArrayList;

/*
数组的长度不能改变
但是ArrayList的长度是可以发生变化的

对于ArrayList来说有一个<E>代表泛型
泛型:也就是装在集合当中的所有元素,全都是统一类型
注意:泛型只能是引用类型,不能是基本类型
 */

import java.util.ArrayList;
import java.util.Arrays;

public class Demo1 {
    public static void main(String[] args) {
        //创建一个名字为list的ArrayList集合
        //里面全部装的是String类型的数据
        ArrayList<String> list =new ArrayList<>();

        //向集合中添加数据 需要用到add方法
        //添加进去的元素与创建时的元素类型相同
        list.add("赵丽颖");
        System.out.println(list);

        list.add("张学友");
        list.add("刘德华");
        list.add("赵本山");
        System.out.println(list);

    }

}

=====================================================
输出结果:
[赵丽颖]
[赵丽颖, 张学友, 刘德华, 赵本山]

ArrayList的其他方法

package com.wang.API.ArrayList;

import java.util.ArrayList;

/*
ArrayList 常用方法

public boolean add(E e): 向集合中添加元素,参数的类型和泛型一致,返回值是是否成功,就是true or false
public E get(int index):从集合中获取元素,参数是索引编号,返回值就是对应位置的元素,从0开始
public E remove(int index):从集合中删除元素,参数是索引编号,返回值就是被删除掉的元素
public int size();获取集合的尺寸长度,返回值是集合中包含的元素个数

 */
public class Demo2 {
    public static void main(String[] args) {
        ArrayList<String> list =new ArrayList<>();

        //向集合中添加元素
        list.add("刘岩");
        list.add("赵丽颖");
        list.add("赵又廷");
        list.add("李小璐");
        System.out.println(list);

        //获取元素
        System.out.println(list.get(2));

        //从集合中删除元素
        String name = list.remove(3);
        System.out.println("删除的人是:"+name);
        System.out.println(list);

        //获取集合的长度
        System.out.println("集合的长度是:"+list.size());

        //遍历集合 与遍历数组相像
        for (int i = 0;i < list.size();i++){
            System.out.print(list.get(i)+" ");
        }

    }
}

Arrays

输出数组:

Arrays.toString(数组名);

数组从小到大排序:

Arrays.sort(数组名);

Integer

valueof 得到Integer

int和String的相互转换

int转String

int num = 100;

方法一

String s1 = “” + num;

方法二

String s2 = String.valueof(num);

String转int

方式一

String ---- Integer ------ int

String s = “100”;

Integer i = Integer.valueof(s);

int s1 = i.intvalue();

方式二

直接转换

int s2 = Integer.parseInt(s);

字符串中的数据排序

1.定义一个字符串

2.把字符串中的数据导入到int数组中

​ 2.1 String[] 新的字符串数组的名字 = 字符串名.split(" ");这里代表以空格分隔开之前的字符,根据()里面的不同,选择分割开字符的不同

​ 2.2 将String[]中的每一个元素转化为int类型存储在一个int[]中

3.Arrays.sort{数组名}对数组进行排序

4,转化为String

package Arrays;

import java.util.Arrays;

public class ArrarDemo2 {
    public static void main(String[] args) {

        String s ="98 55 102 35 11 79";

        String[] stringArray = s.split(" ");
        int[] array = new int[stringArray.length];
        for (int i = 0;i< stringArray.length;i++){
            array[i] = Integer.parseInt(stringArray[i]);
        }
        Arrays.sort(array);
        for (int i = 0;i< array.length;i++){
            System.out.print(array[i]+" ");
        }

    }
}

Date 类

Date d = new Date();

当前时间

直接输出d 显示当前时间

getTime 方法

d.getTime() 方法 返回1970年1月1日到现在的毫米值

setTime 方法

d.setTime(一个数据) 给的数据是毫秒值,输出的时间是从1970年1月1日开始加上输入的毫秒值所达到的时间

Math

  • abs方法 返回绝对值
  • max min 两数最大值 最小值
  • pow(double a,double b) 返回a的b次幂

System

  • exit 方法 终止java虚拟机

  • currentTimeMillis 显示现在时间距离1970年1月1日的毫秒值

    可以用来显示程序运行的时间

    例如:

    package com.wang.API.Math;
    
    public class Demo {
        public static void main(String[] args) {
            long start = System.currentTimeMillis();
            for (int i = 0;i<10000;i++){
                System.out.println(i);
            }
            long end = System.currentTimeMillis();
            System.out.println("共耗时:"+(end - start)+"毫秒");
        }
    }
    
    

Object

  • toString 简便地输出该对象代表的简单的字符串
  • equals() 方法 相同与否比较 例如s1.equals(s2)

冒泡排序

一种排序方式,两个相邻的数据两两比较,大的数据向后放,最后数据从小到大排序

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值