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

 

        } 

    } 

}

转载于:https://www.cnblogs.com/wylwyl/p/10226439.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值