Java上机注意

本文将涉及

1.区分Scanner类中的next()和nextLine()
2.判断实例对象所属类
3.格式化输出

1.区分Scanner类中的next()和nextLine()

next()方法读取到空白符就结束;
nextLine()读取到回车结束也就是"\r"
一般机试中使用nextLine操作即可。

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        System.out.println("next:" + str);
    }
//输出为:
1 2
next:1
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        System.out.println("nextLine:" + str);
    }
//输出为:
1 2
nextLine:1 2

一般机试中采用如下格式:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            String str = sc.nextLine();
            //对str进行一系列操作
        }
    }

2.判断实例对象所属类

一般有两种方法:

  1. 使用instance of:
    boolean flag = (对象名 instance of 类名)。可以返回一个boolean类型的值
  2. 使用class:
    对象使用
    一般的出现的情况是区分子类之间的关系。如Employee类和Manager类继承了同一个类Person.
    某一个方法中参数为Person person(这里Person就像是抽象出来的接口一样),要判断具体的实例是Employee和Manager。
    另外一种少见的情况是判断子类父类之间的关系,从输出结果中可以看出,使用instance of 。子类对象是父类的实例,父类对象不是子类的实例
public class Person {
    String name;
    int age;
}

public class Employee extends Person{
    String type;
}

public class Manager extends Person {
    String manger;
}

public class Test {

    public static void main(String[] args) {
        Person p = new Person();
        Manager m = new Manager();
        Employee e = new Employee();
        print(m);
        print(e);

        //判断子类父类之间的关系
        System.out.println(e instanceof Person);
        System.out.println(p instanceof Employee);
        System.out.println(e.getClass().equals(Person.class));
        System.out.println(p.getClass().equals(Employee.class));
    }

    public static void print(Person person) {
        if (person instanceof Employee) {
            System.out.println("instanceof employee");
        }
        if (person instanceof  Manager) {
            System.out.println("instanceof manager");
        }
        if (person.getClass().equals(Employee.class)) {
            System.out.println("class employee");
        }
        if (person.getClass().equals(Manager.class)) {
            System.out.println("classs manager");
        }
    }
}

输出结果为:

instanceof manager
classs manager
instanceof employee
class employee
true
false
false
false

3.格式化输出

机试中遇到输出二维数组,要求一定的长度w,保留d位小数的要求
使用System.out.format()来输出即可。规则类似c语言的输入输出
%为参数的替代
-表示输出结果左对齐,默认是右对齐
w为长度
.df为保留d位小数
如:System.out.format("%-3.3f",this.nums[i][j]);表示长度为3,输出结果保留3位小数

/**
     * 打印函数
     * 起始一个空行,结束一个空行
     *
     * @param w 每列的宽度
     * @param d 保留的小数点位数
     */
    public void print(int w, int d) {
        System.out.println();
        for (int i = 0; i < this.nums.length; i++) {
            for (int j = 0; j < this.nums[0].length; j++) {
                System.out.format("%-" + w + "." + d + "f",this.nums[i][j]);
            }
            System.out.println();
        }
    }

输出结果为:

1.568     1.000     1.000     
2.000     2.000     2.000     
3.000     3.000     3.000 

你可能感兴趣的:(Java上机注意)