日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

【Java】7.1 与用户互动 7.2 系统相关

發布時間:2025/3/20 windows 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Java】7.1 与用户互动 7.2 系统相关 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

Scanner類

System類

Runtime類


Scanner類

重要的是三個方法:

  • nextInt() : it only reads the int value, nextInt() places the cursor in the same line after reading the input.
    (此方法只讀取整型數值,并且在讀取輸入后把光標留在本行
  • 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.(讀取輸入直到遇見空格。此方法不能讀取被空格分隔開的內容,并且在讀取輸入后把光標留在本行
  • 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,讀取完成后光標被放在下一行
  • 總結:

  • next() 方法(包括next()、nextInt()、nextFloat()等等,除nextLine()外,一定要讀取到有效字符之后才可以結束輸入,有效字符之前遇到的空格、Tab鍵或Enter鍵等結束符會自動將其去掉,有效字符之后遇到的Enter鍵才將其視為結束符,所以next()方法不能得到帶空格的字符串。結束讀取后,光標不移動到下一行!!!
  • 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類

    主要熟悉一下兩個方法

  • identityHashCode(x) :?返回唯一的對x對象的標識值
  • 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========99162322System.out.println(System.identityHashCode(s1)+"======="+System.identityHashCode(s2)); //輸出:1163157884=======1956725890String 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());} }

    ?

    總結

    以上是生活随笔為你收集整理的【Java】7.1 与用户互动 7.2 系统相关的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。