Java输入输出模板

Java输入输出模板

题目

  1. 读测试文件txt和控制台输入共存的写法
    测试文件txt中可以提前准备测试用例,来达到快速测试的目的(控制台输入时间较长,每次测试用例无法保存)。
public class Solution {
   public static void main(String[] args) {
       Scanner sc;
       try {
           File f = new File( "F:\\idea_prj\\Solution\\src\\input.txt" );
           sc = new Scanner(f);//从文件接收数据

       } catch (Exception e) {
           //从控制台输入接收数据
           sc = new Scanner(System.in);
       }
       
       int N=sc.nextInt();//总共有N个数字
       int sum=0;
       for (int i=0;i<N;i++){
           sum+=sc.nextInt();
       }
       System.out.println(sum);
   }
}
  1. 复杂输入问题
    输入:
    [ 1 2 3 4 5 6](前后用 [ ] 封闭,中间间隔不定数目的空格)
public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //若要连续输入多行,可以使用下面的while
        //while (sc.hasNext()) 
        {
            //trim去掉字符串开头与结尾的空格、制表符等
            String string = sc.nextLine().trim();
            //去掉两边的括号,substring()起始索引封闭,终止索引不封闭
            //split()的输入参数可以是一个正则表达式,这里代表以若干个空格进行分隔
            String[] strs = string.substring(1,string.length()-1).split(" +");
            //这里的操作以简单的遍历输出有效字符串示例
            for(String s:strs){
                //去除[ 与数字之间的空格产生的空字符串
                if(s.length()>0)
                    System.out.println(s);
            }
        }
    }
}

接着是牛客网的输入输出题目

  1. 计算a+b:
    输入包括两个正整数 a,b,输入数据包括多组(也就是多行)。
    逐行打印输出a+b的结果。
public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a+b);
        }
    }
}
  1. 计算a+b:
    输入第一行包括一个数据组数 t ,接下来 t 行数据,每行数据包括两个正整数 a,b。
    逐行打印输出a+b的结果。
public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a+b);
        }
    }
}
  1. 计算a+b:
    每行输入包括两个正整数a,b,输入数据有多组,如果输入为0 0则结束输入。
    逐行打印输出a+b的结果。
public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            if(a==0 && b==0)
                break;
            System.out.println(a+b);
        }
    }
}
  1. 计算一系列数的和:
    输入数据包括多组。
    每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
    接下来n个正整数,即需要求和的每个正整数。
    逐行打印输出a+b的结果。
//这段代码没有考虑错误输入(n与之后的数字个数不匹配)的问题,需要改进
public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            if(n==0)
                break;
            int sum = 0;
            while(n-->0){
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}
  1. 计算一系列数的和:
    输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数,接下来t行, 每行一组数据。
    每行的第一个整数为整数的个数n(1 <= n <= 100),接下来n个正整数, 即需要求和的每个正整数。
    打印求和结果,逐行输出。
public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int n = sc.nextInt();
            int sum = 0;
            while(n-->0)
                sum += sc.nextInt();
            System.out.println(sum);
        }
    }
}
  1. 计算一系列数的和:
    输入数据有多组, 每行表示一组输入数据。
    每行的第一个整数为整数的个数n(1 <= n <= 100)。
    接下来n个正整数, 即需要求和的每个正整数。(与前一题相比少了停止条件而已)
    逐行打印输出a+b的结果。
public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int sum = 0;
            while(n-->0)
                sum += sc.nextInt();
            System.out.println(sum);
        }
    }
}
  1. 计算一系列数的和:

输入数据有多组, 每行表示一组输入数据。每行不定有n个整数,空格隔开。(1 <= n <= 100)。
逐行打印输出a+b的结果。
(这里数字个数不定,则需要使用 nextLine() 读取整行序列,然后使用 split() 分割出数字字符串,最后使用 Integer.parseInt() 来转换成数字进行加法)

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String[] nums = sc.nextLine().trim().split(" +");
            int sum = 0;
            for(String num :nums)
                sum += Integer.parseInt(num);
            System.out.println(sum);
        }
    }
}
  1. 对输入的字符串进行排序后输出
    输入有两行,第一行n,第二行是n个空格隔开的字符串。
    输出一行排序后的字符串,空格隔开,无结尾空格。
    (无结尾空格只需要控制好遍历边界即可)
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n= sc.nextInt();
        String[] strs = new String[n];
        for(int i = 0; i<n; i++){
            strs[i] = sc.next();
        }
        Arrays.sort(strs);
        for(int i = 0; i<n-1; i++){
            System.out.print(strs[i]+" ");
        }
        //输出最后一个字符串时需要使用println()
        System.out.println(strs[n-1]);
    }
}
  1. 对输入的字符串进行排序后输出
    输入多个测试用例,每个测试用例一行。每行通过空格隔开,有n个字符,n<100。
    输出多行行排序过的字符串,每个字符串通过空格隔开(末位无空格)
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String[] strs = sc.nextLine().trim().split(" +");
            Arrays.sort(strs);
            for(int i=0;i<strs.length-1;i++)
                System.out.print(strs[i]+" ");
           //输出最后一个字符串时需要使用println()
           System.out.println(strs[strs.length-1]);
        }
    }
}
  1. 对输入的字符串进行排序后输出
    输入多个测试用例,每个测试用例一行。每行通过 , 隔开,有n个字符,n<100。
    输出多行排序后的字符串,用 ','隔开,无结尾空格或 ‘,’ 。
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String[] strs = sc.nextLine().trim().split(",+");
            Arrays.sort(strs);
            for(int i=0;i<strs.length-1;i++)
                System.out.print(strs[i]+",");
            System.out.println(strs[strs.length-1]);
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值