欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java Scanner類用法及nextLine()產(chǎn)生的換行符問題實(shí)例分析

 更新時(shí)間:2019年03月19日 10:58:17   作者:白楊-M  
這篇文章主要介紹了Java Scanner類用法及nextLine()產(chǎn)生的換行符問題,結(jié)合實(shí)例形式分析了Scanner類功能、hasNextInt()和nextInt()方法使用及nextLine()產(chǎn)生的換行符問題解決方法,需要的朋友可以參考下

本文實(shí)例講述了Java Scanner類用法及nextLine()產(chǎn)生的換行符問題。分享給大家供大家參考,具體如下:

分析理解:Scanner sc = new Scanner(System.in);

package cn.itcast_01;
/*
 * Scanner:用于接收鍵盤錄入數(shù)據(jù)。
 *
 * 前面的時(shí)候:
 *     A:導(dǎo)包
 *     B:創(chuàng)建對(duì)象
 *     C:調(diào)用方法
 *
 * 分析理解:Scanner sc = new Scanner(System.in);
 * System類下有一個(gè)靜態(tài)的字段:
 *     public static final InputStream in; 標(biāo)準(zhǔn)的輸入流,對(duì)應(yīng)著鍵盤錄入。
 *
 *     InputStream is = System.in;
 *
 * class Demo {
 *     public static final int x = 10;
 *     public static final Student s = new Student();
 * }
 * int y = Demo.x;
 * Student s = Demo.s;
 *
 *
 * 構(gòu)造方法:
 *     Scanner(InputStream source)
 */
import java.util.Scanner;
public class ScannerDemo {
  public static void main(String[] args) {
    // 創(chuàng)建對(duì)象
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();
    System.out.println("x:" + x);
  }
}

Scanner類的hasNextInt()nextInt()方法

package cn.itcast_02;
import java.util.Scanner;
/*
 * 基本格式:
 *     public boolean hasNextXxx():判斷是否是某種類型的元素
 *     public Xxx nextXxx():獲取該元素
 *
 * 舉例:用int類型的方法舉例
 *     public boolean hasNextInt()
 *     public int nextInt()
 *
 * 注意:
 *     InputMismatchException:輸入的和你想要的不匹配
 */
public class ScannerDemo {
  public static void main(String[] args) {
    // 創(chuàng)建對(duì)象
    Scanner sc = new Scanner(System.in);
    // 獲取數(shù)據(jù)
    if (sc.hasNextInt()) {
      int x = sc.nextInt();
      System.out.println("x:" + x);
    } else {
      System.out.println("你輸入的數(shù)據(jù)有誤");
    }
  }
}

Scanner類中的nextLine()產(chǎn)生的換行符問題

package cn.itcast_03;
import java.util.Scanner;
/*
 * 常用的兩個(gè)方法:
 *     public int nextInt():獲取一個(gè)int類型的值
 *     public String nextLine():獲取一個(gè)String類型的值
 *
 * 出現(xiàn)問題了:
 *     先獲取一個(gè)數(shù)值,在獲取一個(gè)字符串,會(huì)出現(xiàn)問題。
 *     主要原因:就是那個(gè)換行符號(hào)的問題。
 * 如何解決呢?
 *     A:先獲取一個(gè)數(shù)值后,在創(chuàng)建一個(gè)新的鍵盤錄入對(duì)象獲取字符串。
 *     B:把所有的數(shù)據(jù)都先按照字符串獲取,然后要什么,你就對(duì)應(yīng)的轉(zhuǎn)換為什么。
 */
public class ScannerDemo {
  public static void main(String[] args) {
    // 創(chuàng)建對(duì)象
    Scanner sc = new Scanner(System.in);
    // 獲取兩個(gè)int類型的值
    // int a = sc.nextInt();
    // int b = sc.nextInt();
    // System.out.println("a:" + a + ",b:" + b);
    // System.out.println("-------------------");
    // 獲取兩個(gè)String類型的值
    // String s1 = sc.nextLine();
    // String s2 = sc.nextLine();
    // System.out.println("s1:" + s1 + ",s2:" + s2);
    // System.out.println("-------------------");
    // 先獲取一個(gè)字符串,在獲取一個(gè)int值
    // String s1 = sc.nextLine();
    // int b = sc.nextInt();
    // System.out.println("s1:" + s1 + ",b:" + b);
    // System.out.println("-------------------");
    // 先獲取一個(gè)int值,在獲取一個(gè)字符串,這里會(huì)出問題
    // int a = sc.nextInt();
    // String s2 = sc.nextLine();
    // System.out.println("a:" + a + ",s2:" + s2);
    // System.out.println("-------------------");
    int a = sc.nextInt();
    Scanner sc2 = new Scanner(System.in);
    String s = sc2.nextLine();
    System.out.println("a:" + a + ",s:" + s);
  }
}

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論