zoukankan      html  css  js  c++  java
  • 校招小白机考入坑之从键盘输入java的各种数据类型

    //1.从键盘输入一个整型(其他基本类型类似)
    Scanner sc =new Scanner(System.in);
    sc.hasNextInt();
    int str1 = sc.nextInt();
    
    //2.从键盘输入一个定长的整型数组
    int[] arr = new int[21];
    Scanner sc = new Scanner(System.in);        
    for (int i=0;i<21;i++){      
    sc.hasNextInt();
    arr[i]=sc.nextInt();
     }
    
    //3.从键盘输入一个字符串(没有空格)
    Scanner sc =new Scanner(System.in);
    sc.hasNext();
    String str1 = sc.next();
    
    //4.从键盘输入一行字符串(可以包含空格)
    Scanner sc =new Scanner(System.in);
    sc.hasNextLine();
    String str1 = sc.nextLine();
    
    //5.从键盘输入一行以逗号隔开的整型数组,包含负整型,且不定长
    //  如  1,2,-3,4,-5,6 ...    
    // 注意Integer.parseInt不能解析负整
    型的字符串,真的坑,那时本小白考试最后一道就败在这上面,一把辛酸泪 Scanner sc = new Scanner(System.in); sc.hasNextLine(); String[] str = sc.nextLine().split(","); int[] arr = new int[str.length]; for (int i=0;i<str.length;i++){ if (str[i].startsWith("-")){ arr[i]=-Integer.parseInt(str[i].substring(1)); }else { arr[i]=Integer.parseInt(str[i]); } }

    都是一个一个坑踩过来的个人经验,小徐希望能帮到幸运的小白们。

    小徐看世界,世界如此多娇: http://www.cnblogs.com/schoolbag/

  • 相关阅读:
    char与unsigned char的区别
    C语言 —— sprintf()函数
    char *s 与 char s[ ]的区别
    打印不同对象的字节表示 ( 对int*强制转换成unsigned char*的理解 )
    洛谷P2242 公路维修问题(Road)
    洛谷P1209 [USACO1.3]修理牛棚 Barn Repair
    洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication
    洛谷P2246 SAC#1
    Bzoj4300 绝世好题
    Uva1398 Meteor
  • 原文地址:https://www.cnblogs.com/schoolbag/p/8672068.html
Copyright © 2011-2022 走看看