zoukankan      html  css  js  c++  java
  • Java-3 Scanner用法

    1.Scanner用法

    • 获取键盘输入的信息。
    import java.util.Scanner;
    class ScannerDemo{
    	public static void main(String[] args){
    		// 创建对象
    		Scanner sc = new Scanner(System.in);
    		// 获取键盘录入信息
    		/**
    		.nextShort();
    		.nextLong();
    		.nextFloat();//获取小数
    		.nextDouble();
    		.nextLine(); //获取字符串
    		*/
    		System.out.println("请输入你的姓名:");
    		String name = sc.nextLine();
    		System.out.println("请输入你的年龄:");
    		int age = sc.nextInt();
    		System.out.println("你好,"+name+"的年龄为"+age);
    	}
    }
    
    • 需要注意是:在程序中既使用了nextInt,还使用nextLine,我们必须把nextLine放到nextInt的上面,否则会导致nextLine没法继续输入,如果非要把nextInt放在下面,把nextLine改成next即可。
    System.out.println("请输入你的年龄:");
    int age = sc.nextInt();
    System.out.println("请输入你的姓名:");
    String name = sc.next();
    System.out.println("你好,"+name+"的年龄为"+age);
    
  • 相关阅读:
    hdu 1087(LIS变形)
    poj 1088(记忆化搜索)
    hdu 1505(最大子矩阵)
    hdu 1506(好题+DP或者RMQ)
    poj 2593&&poj2479(最大两子段和)
    hdu 1003(最大子段和)
    hdu 2881(LIS变形)
    poj 1692(动态规划)
    CodeForces 626C Block Towers
    CodeForces 626B Cards
  • 原文地址:https://www.cnblogs.com/xujunkai/p/13698028.html
Copyright © 2011-2022 走看看