【Java】7.1 與用戶互動 & 7.2 系統相關

目錄

Scanner類

System類

Runtime類


Scanner類

重要的是三個方法:

  1. nextInt() : it only reads the int value, nextInt() places the cursor in the same line after reading the input.
    (此方法只讀取整型數值,並且在讀取輸入後把光標留在本行
  2. next() : read the input only till the space. It can’t read two words separated by space. Also, next() places the cursor in the same line after reading the input.(讀取輸入直到遇見空格。此方法不能讀取被空格分隔開的內容,並且在讀取輸入後把光標留在本行
  3. nextLine(): reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.(讀取包括空格在內的輸入,而且還會讀取行尾的換行字符\n,讀取完成後光標被放在下一行

總結:

  1. next() 方法(包括next()、nextInt()、nextFloat()等等,除nextLine()外,一定要讀取到有效字符之後纔可以結束輸入,有效字符之前遇到的空格、Tab鍵或Enter鍵等結束符會自動將其去掉,有效字符之後遇到的Enter鍵纔將其視爲結束符,所以next()方法不能得到帶空格的字符串。結束讀取後,光標不移動到下一行!!!
  2. nextLine() 方法,返回的是Enter鍵之前的所有字符可以得到帶空格的字符串。結束一行讀取後,光標移到下一行!!!

 

import java.util.Arrays;
import java.util.Scanner;

/**
 * @ClassName: ScannerTest
 * @description:
 * @author: FFIDEAL
 * @Date: 2020/3/13 12:35
 */

public class ScannerTest {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        //讀取換行符,若沒有下面一行語句,會少一個值
        scanner.nextLine();
        String[] array = new String[n];
        for(int i = 0; i < n; i++){
            array[i] = scanner.nextLine();
        }
        System.out.println(Arrays.toString(array));
    }
}
import java.util.Scanner;

/**
 * @ClassName: ScannerKeyBoardTest
 * @description:學習Scanner獲取鍵盤輸入
 * @author: FFIDEAL
 * @Date: 2020/3/5 9:38
 */

public class ScannerKeyBoardTest {
    public static void main(String[] args){
        //設置一個掃描器
        Scanner sc = new Scanner(System.in);
        //獲取鍵盤上的輸入
        //hasNextLine():逐行讀取,返回值爲boolean
        //nextLine():返回輸入源中的下一行,返回值爲String

        //判斷是否還有下一行
        while(sc.hasNextLine()){
            System.out.println("鍵盤輸入的內容是:"+sc.nextLine());
        }
    }
}

 【用Scanner類讀取文件】

import java.io.File;
import java.util.Scanner;

/**
 * @ClassName: ScannerFileTest
 * @description:Scanner獲取文件中的內容
 * @author: FFIDEAL
 * @Date: 2020/3/5 10:06
 */

public class ScannerFileTest {
    public static void main(String[] args) throws Exception{
        //將一個File對象作爲Scanner構造器的參照物,用Scanner讀取文件內容
        Scanner sc = new Scanner(new File("ScannerKeyBoardTest.java"));
        System.out.println("ScannerKeyBoardTest的內容如下");
        while(sc.hasNextLine()){
            System.out.println(sc.nextLine());
        }
    }
}

System類

主要熟悉一下兩個方法

  1. identityHashCode(x) : 返回唯一的對x對象的標識值
  2. hashCode() : 不能唯一標識該對象
/**
 * @ClassName: IdentityHashCodeTest
 * @description:HashCode和identityHashCode初解。identityHashCode是對象的唯一標識碼
 * @author: FFIDEAL
 * @Date: 2020/3/5 10:38
 */

public class IdentityHashCodeTest {
    public static void main(String[] args){
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.println(s1.hashCode()+"========"+s2.hashCode());     
//輸出:99162322========99162322
        System.out.println(System.identityHashCode(s1)+"======="+System.identityHashCode(s2));      
//輸出:1163157884=======1956725890

        String s3 = "Java";
        String s4 = "Java";
        System.out.println(System.identityHashCode(s3)+"======="+System.identityHashCode(s4));      
//輸出:356573597=======356573597
    }
}

Runtime類

Runtime類代表Java程序的運行時環境,可以訪問JVM的相關信息,如處理器數量,內存信息

/**
 * @ClassName: RuntimeTest
 * @description:
 * @author: FFIDEAL
 * @Date: 2020/3/5 10:46
 */

public class RuntimeTest {
    public static void main(String[] args){
        //獲取java程序關聯的運行時對象
        Runtime rt = Runtime.getRuntime();
        System.out.println("處理機數量:"+rt.availableProcessors());
        System.out.println("空閒內存數:"+rt.freeMemory());
        System.out.println("總內存數:"+rt.totalMemory());
        System.out.println("可用內存最大數:"+rt.maxMemory());
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章