<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java鍵盤錄入涉及到的方法

          共 3508字,需瀏覽 8分鐘

           ·

          2023-07-31 20:26

          一,鍵盤錄入涉及到的方法如下:

          next()、nextLine()、nextInt()、nextDouble()。

          1)next()、nextLine():

          可以接受任意數(shù)據(jù),但是都會返回一個字符串。

          比如:鍵盤錄入abc,那么會把abc看做字符串返回。

          鍵盤錄入123,那么會把123看做字符串返回。

          代碼示例:

          Scanner sc = new Scanner(System.in);
          String s = sc.next();//錄入的所有數(shù)據(jù)都會看做是字符串
          System.out.println(s);

          代碼示例:

          Scanner sc = new Scanner(System.in);
          String s = sc.nextLine();//錄入的所有數(shù)據(jù)都會看做是字符串
          System.out.println(s);

          2)nextInt():

          只能接受整數(shù)。

          比如:鍵盤錄入123,那么會把123當(dāng)做int類型的整數(shù)返回。

          鍵盤錄入小數(shù)或者其他字母,就會報錯。

          代碼示例:

          Scanner sc = new Scanner(System.in);
          int s = sc.nextInt();//只能錄入整數(shù)
          System.out.println(s);

          3)nextDouble():

          能接收整數(shù)和小數(shù),但是都會看做小數(shù)返回。

          錄入字母會報錯。

          代碼示例:

          Scanner sc = new Scanner(System.in);
          double d = sc.nextDouble();//錄入的整數(shù),小數(shù)都會看做小數(shù)。
                //錄入字母會報錯
          System.out.println(d);

          二,方法底層細(xì)節(jié) :

          第一個細(xì)節(jié):

          next(),nextInt(),nextDouble()在接收數(shù)據(jù)的時候,會遇到空格,回車,制表符其中一個就會停止接收數(shù)據(jù)。

          代碼示例:

          Scanner sc = new Scanner(System.in);
          double d = sc.nextDouble();
          System.out.println(d);
          //鍵盤錄入:1.1 2.2//注意錄入的時候1.1和2.2之間加空格隔開。
          //此時控制臺打印1.1
          //表示nextDouble方法在接收數(shù)據(jù)的時候,遇到空格就停止了,后面的本次不接收。
          Scanner sc = new Scanner(System.in);
          int i = sc.nextInt();
          System.out.println(i);
          //鍵盤錄入:1 2//注意錄入的時候1和2之間加空格隔開。
          //此時控制臺打印1
          //表示nextInt方法在接收數(shù)據(jù)的時候,遇到空格就停止了,后面的本次不接收。
          Scanner sc = new Scanner(System.in);
          String s = sc.next();
          System.out.println(s);
          //鍵盤錄入:a b//注意錄入的時候a和b之間加空格隔開。
          //此時控制臺打印a
          //表示next方法在接收數(shù)據(jù)的時候,遇到空格就停止了,后面的本次不接收。

          第二個細(xì)節(jié):

          next(),nextInt(),nextDouble()在接收數(shù)據(jù)的時候,會遇到空格,回車,制表符其中一個就會停止接收數(shù)據(jù)。但是這些符號 + 后面的數(shù)據(jù)還在內(nèi)存中并沒有接收。如果后面還有其他鍵盤錄入的方法,會自動將這些數(shù)據(jù)接收。

          代碼示例:

          Scanner sc = new Scanner(System.in);
          String s1 = sc.next();
          String s2 = sc.next();
          System.out.println(s1);
          System.out.println(s2);
          //此時值鍵盤錄入一次a b(注意a和b之間用空格隔開)
          //那么第一個next();會接收a,a后面是空格,那么就停止,所以打印s1是a
          //但是空格+b還在內(nèi)存中。
          //第二個next會去掉前面的空格,只接收b
          //所以第二個s2打印出來是b

          第三個細(xì)節(jié):

          nextLine()方法是把一整行全部接收完畢。

          代碼示例:

          Scanner sc = new Scanner(System.in);
          String s = sc.nextLine();
          System.out.println(s);
          //鍵盤錄入a b(注意a和b之間用空格隔開)
          //那么nextLine不會過濾前面和后面的空格,會把這一整行數(shù)據(jù)全部接收完畢。

          三、混用引起的后果

          上面說的兩套鍵盤錄入不能混用,如果混用會有嚴(yán)重的后果。

          代碼示例:

          Scanner sc = new Scanner(System.in);//①
          int i = sc.nextInt();//②
          String s = sc.nextLine();//③
          System.out.println(i);//④
          System.out.println(s);//⑤

          當(dāng)代碼運行到第二行,會讓我們鍵盤錄入,此時錄入123。

          但是實際上我們錄的是123+回車。

          而nextInt是遇到空格,回車,制表符都會停止。

          所以nextInt只能接受123,回車還在內(nèi)存中沒有被接收。

          此時就被nextLine接收了。

          所以,如果混用就會導(dǎo)致nextLine接收不到數(shù)據(jù)。

          四、結(jié)論(如何使用)

          鍵盤錄入分為兩套:

          • next()、nextInt()、nextDouble()這三個配套使用。

          如果用了這三個其中一個,就不要用nextLine()。

          • nextLine()單獨使用。

          如果想要整數(shù),那么先接收,再使用Integer.parseInt進(jìn)行類型轉(zhuǎn)換。

          代碼示例:

          Scanner sc = new Scanner(System.in);
          String s = sc.next();//鍵盤錄入123
          System.out.println("此時為字符串" + s);//此時123是字符串
          int i = sc.nextInt();//鍵盤錄入123
          System.out.println("此時為整數(shù):" + i);
          Scanner sc = new Scanner(System.in);
          String s = sc.nextLine();//鍵盤錄入123
          System.out.println("此時為字符串" + s);//此時123是字符串
          int i = Integer.parseInt(s);//想要整數(shù)再進(jìn)行轉(zhuǎn)換
          System.out.println("此時為整數(shù):" + i);


          瀏覽 91
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  草草地址线路①屁屁影院成人 | 综合五月丁香视频 | 在线观看欧美日韩视频 | 丁香五月激情婷婷 | 多人p|