zoukankan      html  css  js  c++  java
  • 06--Java--Scanner类读入控制台

    Scanner类读入控制台

      1.什么是Scanner类

         Scanner类是java中从控制台读入用户输入的类  

     1 import java.util.Scanner;
     2 
     3 public class a_Learn_Scanner {
     4     public static void main(String[] args){
     5         String name;
     6         int age;
     7         Scanner sc = new Scanner(System.in);
     8         System.out.print("请输入您的姓名:");
     9         name = sc.next();
    10         
    11         System.out.print("请输入您的年龄:");
    12         age = sc.nextInt();
    13         
    14         System.out.println("姓名:"+name+"   年龄:"+age);
    15     }
    16 }

        2.Scanner类使用步骤

         1.导入包              import java.util.Scanner;

         2.创建Scanner对象    Scanner sc = new Scanner(System.in);

         3.调用对象方法,实现读取    age =  sc.nextInt();

       3.Scanner类的常用方法  

                        

       4.Scanner类的注意事项

          1.next()方法: 它读入的内容不包含有效字符前的空格,在输入有效字符后的空格、Tab、回车作为分隔符或结束符

          2.nextLine()方法: 它读入的内容包含有效字符前的空格,并只以回车作为结束符

          3.hasNext()方法和hasNextLine()用于判断是否读入结束,它可用于在读文件结尾结束或是做ACM题的时候遇到“存在多组测试数据”的时候使用。

                例如:

    1 Scanner sc = new Scanner(System.in);
    2 while(sc.hasNext()){
    3         int n = sc.nextInt();
    4     System.out.println("exit fail!");
    5 }
    6 System.out.println("exit success!");

             4.String[] numstr = sc.nextLine().trim().split("\s+")用来输入一行数据,并用空格间隔,读入到numstr字符串数组中

          5.注意事项慢慢积累,欢迎评论留言

  • 相关阅读:
    faster with MyISAM tables than with InnoDB or NDB tables
    w-BIG TABLE 1-toSMALLtable @-toMEMORY
    Indexing and Hashing
    MEMORY Storage Engine MEMORY Tables TEMPORARY TABLE max_heap_table_size
    controlling the variance of request response times and not just worrying about maximizing queries per second
    Variance
    Population Mean
    12.162s 1805.867s
    situations where MyISAM will be faster than InnoDB
    1920.154s 0.309s 30817
  • 原文地址:https://www.cnblogs.com/qinqin-me/p/12256442.html
Copyright © 2011-2022 走看看