码迷,mamicode.com
首页 > 编程语言 > 详细

Java算法测试的输入模板

时间:2019-01-05 22:39:14      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:实例   多个   测试   inpu   java   add   scanner   exti   main   

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();

 

        } 

    } 

}

Java算法测试的输入模板

标签:实例   多个   测试   inpu   java   add   scanner   exti   main   

原文地址:https://www.cnblogs.com/wylwyl/p/10226439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!