• Java算法测试的输入模板


    Java数据读入

     

    读入一个整数:

    Scanner sc = new Scanner (System.in);

    int n = sc.nextInt();

     

    读入一个字符串

    Scanner sc = new Scanner (System.in);

    String s=sc.next();

     

    读入一个浮点数

    Scanner sc = new Scanner (System.in);

    double t = sc.nextDouble();

     

    读入一行

    Scanner sc = new Scanner (System.in);

    String s = sc.nextLine();

     

    判断是否有下一个输入sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()

     

    Java读入一行空格隔开的数据

    Sample Input

    1 2 3 4 5 6 7 8 9 10 11 12 13

    import java.util.Scanner;

    public class Demo {

        public static void main(String[] args) {

     

            Scanner sc = new Scanner(System.in);

            String s = sc.nextLine();

     

            String ss[] = s.split(" ");

            int len = ss.length;

     

            int[] src = new int[len];

     

            for(int i = 0; i < len; i++){

                src[i] = Integer.parseInt(ss[i]);

            }

        }

    }

     

     

    读入一个整数

    public class Solution{

        public static void main(String[] args){

            Scanner in = new Scanner(System.in);

            int a = in.nextInt();

        }

    }

    连续数据多个数

        public static void readManyNumber(){

            Scanner in = new Scanner(System.in);

            int len = in.nextInt();

            int[] array = new int[len];

            for(int i = 0; i < len; i++){

                array[i] = in.nextInt();

            }

           

        }

    读入整数

    import java.util.Scanner; 

    public class Main { 

        public static void main(String[] args) { 

            Scanner sc =new Scanner(System.in); 

           

            while(sc.hasNext()){  //判断是否结束 

                int score = sc.nextInt();

               

            }//读入整数 

         

        } 

     

    读入实数

    输入数据有多组,每组占2行,第一行为一个整数N,指示第二行包含N个实数。

    Sample Input 

    4  

    56.9  67.7  90.5  12.8  

    5  

    56.9  67.7  90.5  12.8

    import java.util.Scanner; 

    public class Main { 

        public static void main(String[] args) { 

            Scanner sc =new Scanner(System.in);

     

            while(sc.hasNext()){ 

                int n = sc.nextInt();

                

                for(int i=0;i<n;i++){ 

                    double a = sc.nextDouble();   

                } 

            } 

        } 

     

    读入字符串

    输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

    Sample Input   

    asdfasdf123123asdfasdf 

    asdf111111111asdfasdfasdf

    import java.util.Scanner; 

    public class Main { 

        public static void main(String[] args) {

            Scanner sc = new Scanner(System.in); 

            int n = Integer.parseInt(sc.nextLine());

            for(int i=0;i<n;i++){ 

                String str = sc.nextLine();

     

            } 

        } 

    }

  • 相关阅读:
    C++ Primer Plus(三)
    C++ Primer Plus(二)
    C++ Primer Plus(一)
    C Primer Plus(三)
    C++ 函数重载,函数模板和函数模板重载,选择哪一个?
    Spring IoC 公共注解详解
    Spring IoC @Autowired 注解详解
    Spring IoC 容器的扩展
    Spring IoC bean 的初始化
    Spring IoC 属性赋值阶段
  • 原文地址:https://www.cnblogs.com/wylwyl/p/10226439.html
Copyright © 2020-2023  润新知