java Day 3: Scanner, Random, ArrayList

Chapter One: Scanner from entry to give up


1 API and using the steps outlined

API Application Programming Interface for short, is JDK has provided us with good classes and methods can be used directly, and is readily used by programmers dictionary.
***

2 Scanner Overview

2-1 reference type is generally used in step

1: package guide

  • import package path. class name
  • If you need to import the current class and classes in the same package in the following, the need to import.
  • Only the class does not need the guide java.lang package, other packages require guide.

    2: Creating
    format: name = new class object class name ();

3 Use
Object-member variable
. Object members method ();
***

3 a first example of use of the Scanner

代码块
package demo1;

//导包
import java.util.Scanner;
public class demo {
    public static void main(String[] args) {
//        创建对象
        Scanner scr = new Scanner(System.in);
        System.out.println("请输入数字:");
//        调用方法
        int num = scr.nextInt();
        System.out.println("输入的数字是:"+scr);
    }
}

4 Scanner using two numbers input summing

代码块
package demo1;
import java.util.Scanner;
public class Student {
    public static void main(String[] args) {
        Scanner src = new Scanner(System.in);
        System.out.println("请输入第一个数字:");
        int num = src.nextInt();
        System.out.println("请输入第二个数字:");
        int num2 = src.nextInt();
        System.out.println("两个数字的和是"+(num+num2));
    }

}

Note 5 anonymous object

Anonymous objects used only once, if some object is used only once, then use an anonymous object.

Format: class name ();

Use 5-1 anonymous object

package demo1;

public class demo {
    public static void main(String[] args) {
        new Student().name = "wangsiyu";
        System.out.println(new Student().name);
        思考:为什么打印出来的是空?是因为又重新new了一个匿名对象
    }
}

6 anonymous object as a parameter and return value of the method

Any data type, the method can be used as parameters and return values

6-1 anonymous object as a parameter

代码块
package demo1;
import java.util.Scanner;
public class demo {
    public static void main(String[] args) {
        method1(new Scanner(System.in));
//        这里method可以直接调用,是因为是static静态方法

    }
    public static void method1(Scanner src){
        System.out.println(src.nextInt());
    }
}

6-2 anonymous object as the return value

package demo1;
import java.util.Scanner;
public class demo {
    public static void main(String[] args) {
        Scanner src = method2();
        System.out.println(src.nextInt());
    }
    public  static Scanner method2(){
        return new Scanner(System.in);
    }
}

Chapter Two: Random from entry to give up

7 Random Overview

  • Used to generate random numbers
代码块
package demo1;
import java.util.Random;
public class randomdemo {
    public static void main(String[] args) {
        Random rdm = new Random();
        System.out.println(rdm.nextInt());
    }
}

8 Random generated random numbers specified range

random.nextInt (20) represents a random number between from [0,20)

package demo1;
import java.util.Random;
public class randomdemo {
    public static void main(String[] args) {
        Random rdm = new Random();
        System.out.println(rdm.nextInt(20));
    }
}

9 Random Exercise 1: invoke method requires a numeric keypad to enter n, and then generates random numbers between 1-n

package demo1; import java.util.Random; import java.util.Scanner; /*调用方法,要求键盘输入一个数字n,然后生成1—n之间的随机数* /
Public class randomdemo {
public static void main (String [] args) {
System.out.println ( "Please enter a number n, you will receive a random number-n-between. 1");
Scanner Scanner new new SCR = ( the System.in);
int scr.nextInt RES = ();
Method (RES);
}
public static void Method (n-int) {
System.out.println (new new the Random () the nextInt (n-));.
}
}

# 10 Random练习2:给用户三次机会猜数字,每次提示数字比正确的大还是小,猜大小

the demo1 Package;
Import java.util.Random;
Import java.util.Scanner;
/ * invoke methods, requires a digital keyboard input n, and then generates random numbers between 1-n

public class randomdemo {
    public static void main(String[] args) {
        int finalNum = 429;
        int count = 0;
        System.out.println("请输入一个数字:");
        while (count<3){
            Scanner scr = new Scanner(System.in);
            int num = scr.nextInt();

            if(num>finalNum){
                System.out.println("请输入小一点数字");
                count++;
                continue;
            }
            else if (num<finalNum){
                System.out.println("请输入大一点数字");
                count++;
                continue;
            }
            else {
                System.out.println("恭喜输入正确");
                break;
            }
        }
    }
}

Chapter III: ArrayList

11 array of objects

Define an array, storing three Student object
`` `
Package the demo1;

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

    Student[] array = new Student[3];

    Student one = new Student();
    Student two = new Student();
    Student three = new Student();
    array[0] = one;
    array[0] = two;
    array[0] = three;

    array[0].setName("wangsiyu");
    System.out.println(array[0].getName());
}

}

# 12 ArrayList集合概述
>数组array的长度是不可发生改变的,ArrayList的长度是可变的。

>ArrayList<E>中E代表泛型,泛型只能是引用类型,不可以是基本类型

>格式:ArrayList<String> list = new ArrayList<>();

package demo1;

import java.util.ArrayList;

public class arraylistdemo {
public static void main(String[] args) {
ArrayList list= new ArrayList<>();
list.add("wangsiyu");
list.add("nezha");
list.add("hognhaier");
System.out.println(list);
// [wangsiyu, nezha, hognhaier]
}
}

# 13 ArrayList集合常用方法和遍历
- 添加元素 add()
- 删除元素 remove(index) 
- 查询元素 get(index) 
- 获取集合长度size()

package demo1;

import java.util.ArrayList;

public class arraylistdemo {
public static void main(String[] args) {
ArrayList list= new ArrayList<>();
list.add("wangsiyu");
list.add("nezha");
list.add("hognhaier");
System.out.println(list);
// [wangsiyu, nezha, hognhaier]
list.remove(2);
System.out.println(list.get(0));
System.out.println(list);
}
}

###集合的遍历

for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));

    }
# 14 集合存储基本数据类型的解决办法
>必须使用基本数据的包装类

基本数据类型|包装类
:-|:-|
byte|Byte
short|Short
int|Integer(特殊)
long|Long
float|Float
double|Double
char|Character
boolean|Boolean
***
# 15 练习题1
生成6个1-33之间的随机整数,添加到集合并遍历

the demo1 Package;
// generates a random integer between 6 to 33, and added to the set traverse
Import java.util.Random;
Import of java.util.ArrayList;
public class mystu {
public static void main (String [] args) {
// create a collection
ArrayList the ArrayList = new new List <> ();
for (int I = 0; I <. 6; I ++) {
// create a random number
the Random new new RDM = the Random ();
int RES = rdm.nextInt (32);
List.add (+ RES. 1);
}
System.out.println (List);
}
}

# 16 练习题2 优化15,要求低耦合,可复用,并拿到集合最大值

the demo1 Package;
// generates a random integer between 1-m n number, added to the set and traverse, and to get a set of maximum
Import java.util.Random;
Import of java.util.ArrayList;
public class mystu {
public static void main (String [] args) {
// create a collection
ArrayList = ranMethod finalarray (6,33);
int = finalRes ordermethod (finalarray);
System.out.println ( "collection is a" + finalarray + "is the maximum value in the collection" + finalRes);

}
public static ArrayList<Integer> ranMethod(int n,int m){

// where n represents several, m represents a maximum number of values
ArrayList list = new ArrayList<>();
for (int i = 0; i < n; i++) {
// 创建一个随机数
Random rdm = new Random();
int res = rdm.nextInt(m);
list.add(res+1);
}
return list;
}
public static int orderMethod(ArrayList arrlist){
int max = arrlist.get(0);
for (int i = 0; i < arrlist.size(); i++) {
if(max<arrlist.get(i)){
max = arrlist.get(i);
}
}
return max;
}
}

# 17筛选随机数
要求:大集合存入20个100以内随机数,使用方法,筛选出偶数添加到另一个集合并打印

the demo1 Package;
// into a large set of n random numbers less than 100,
// use, to filter out the even add another set of prints and
Import java.util.Random;
Import of java.util.ArrayList;
public class Demo3 {
public void main static (String [] args) {
System.out.println (methodEven (methodRandom (50)));
}
public static the ArrayList methodRandom (n-int) {
// n-number of representatives
ArrayList list = new ArrayList<>();
Random rdm = new Random();

    for (int i = 0; i < n; i++) {
        int num = rdm.nextInt(100);
        list.add(num);
    }
    return list;
}
public static ArrayList<Integer> methodEven(ArrayList<Integer> list){
    ArrayList<Integer> smallArrayList = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        if(list.get(i)%2==0){
            smallArrayList.add(list.get(i));
        }
    }
    return smallArrayList;
}

}
```

18 learning experience

Today is the third day of learning java, Wow, wearing headphones, listening to music, the sound Jianpanshangqiao attack code, as if I'm in control of this world can do whatever they want, we thought to the place, everything Jieke coding, From time to time the impact of this accomplishment is my Tian Linggai, the world will be good?

Guess you like

Origin www.cnblogs.com/robinjackpony/p/11655663.html