Java Print and Scanner

  1. double input = 3.14159265359;
    System.out.println(“double : " + String.format(”%.2f", input)); //only two decimal print
  2. what is the different between print/printf/println?
    print is regular print. printf is print with format. and println is print+change line.
example of printf: 
System.out.printf("i的值为%d,j的值为%.2f", i,j);

Java Scanner:
Scanner is a class, like arraylist.

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据
 
        // next方式接收字符串
        System.out.println("next方式接收:");
        // 判断是否还有输入
        if (scan.hasNext()) {
            String str1 = scan.next();
            System.out.println("输入的数据为:" + str1);
        }
        scan.close(); //don't forget these!
    }

there are few method scanner commonly use: next(), nextLine(), hasNext(), hasNextInt(), nextInt()

posted @ 2020-04-27 08:05  EvanMeetTheWorld  阅读(22)  评论(0)    收藏  举报