Java-笔试输入输出记录

本文详细介绍了Java中的Scanner类用于输入输出的操作,包括nextInt(), nextDouble(), next()和nextLine()方法的使用,并通过多个示例展示了如何处理数组求和,包括有固定组数、以0为结束标志、连续输入等场景。这些示例有助于理解Scanner类在处理用户输入时的逻辑。
摘要由CSDN通过智能技术生成

春招开始了,希望能有个好的结果,很久没有提笔写博客了,那就从输入输出开始吧!
美团的笔试还是被输入输出坑了,重新整理一下
Scanner:

  • nextInt():输入整数 只读取整数类型数据, nextInt()在读取完输入后把光标放在读取数据的同一行,该数据的后面。当我们一直使用其读数据的时候,此时遇到回车的时候会自动换行,继续读数据。
  • nextDouble():输入双精度数
  • next():输入字符串(以空格作为分隔符)。只读取到空格,不能读取被空格分开的两个单词(也就是不能读取空格),并且在读取完后把光标放在读取数据的同一行,该数据的后面。
  • nextLine():输入字符串(以回车最为分隔符)。读取整行的数据包括单词间的空格,到回车结束(也就是从开始读一整行包括回车),读取结束后,光标放在下一行开头。
/**1.数组求和 一直输入
* 1 5
10 20
*
* 6
 30
* */
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int a = sc.nextInt();// long a=sc.nextLong();
            int b = sc.nextInt();// long b=sc.nextLong();
            System.out.println(a + b);
        }
    }
}



/** 2.数组求和 有组数
 *
2(组数)
1 5
10 20
 *
6
30
 * */
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        while(n>0) {
            int a=sc.nextInt();
            int b = sc.nextInt();
            int sum=a+b;
            System.out.println(sum);
            n--;
        }
    }
}


/** 3.数组 0 0 为结束
 *
1 5
10 20
 0 0
 *
6
30
 * */
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()){
            int a = input.nextInt();
            int b = input.nextInt();
            //!
            if (a == 0 && b == 0){
                break;
            }
            System.out.println(a + b);
        }
    }
}


/**4. 数组求和   0为结束
 *
4 1 2 3 4(4个数 和为1+2+3+4 )
5 1 2 3 4 5
0
 *
10
15
 */


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();
            if (n == 0) {
                return;
            }
            int sum = 0;
            //while (n-- > 0){
            //    sum += input.nextInt();
            //}
            for (int i = 0; i < n; i++) {
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }


    }

}



/** 5. 数组求和   有组数
 *
2(组数)
4 1 2 3 4  (4个数 和为1+2+3+4 )
5 1 2 3 4 5

 *
 *10
15


 */

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int i=0;i<t;i++){
            int num=sc.nextInt();
            int sum=0;
            for(int j=0;j<num;j++){
                sum=sum+sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}
/**6. 数组求和   一直输入
  4 1 2 3 4 (4个数 和为1+2+3+4 )
  5 1 2 3 4 5

   *
   10
   15
*/
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();
            int sum = 0;
            for(int i =0;i<n;i++){
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }

}

/** 7,数组求和 直接求  一直输入
 *
1 2 3
4 5
0 0 0 0 0

*
6
9
0


 */
import java.util.Scanner;
public class Main{
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int sum = 0;
            String[] str =  sc.nextLine().split(" ");
            for(int i=0;i<str.length;i++){
                sum+=Integer.parseInt(str[i]);
            }
            //for(String str:int1){
            //    i+=Integer.valueOf(str);
            //}
            System.out.println(sum);
        }
    }
}


/** 字符串1 有个数
 *
 5(个数)
 c d a bb e
 *
 a bb c d e
 */
import java.util.Scanner;
import java.util.Arrays;
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int num = sc.nextInt();
            String[] s = new String[num];
            for(int i = 0;i < num; i++){
                s[i] = sc.next();
            }
            Arrays.sort(s);
            System.out.println(String.join(" ",s));
        }


    }

}

/** 字符串2 一直输入
 *
 a c bb
 f dddd
 nowcoder
 *
 *
 a bb c
 dddd f
 nowcoder
 */
import java.util.*;
        import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            String[] strs = in.nextLine().split(" ");
            Arrays.sort(strs);
            // System.out.println(String.join(" ",s));
            String res = "";
            for(String s : strs)
                res += s + " ";
            System.out.println(res.trim());
        }
    }
}


/** 字符串3 一直输入 ,号间隔 
 a,c,bb
 f,dddd
 nowcoder
 *
 a,bb,c
 dddd,f
 nowcoder
 */

import java.util.*;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            String[] strs = in.nextLine().split(",");
            Arrays.sort(strs);
            // System.out.println(String.join(",",s));
            String res = "";
            for(String s : strs)
                res += s + ",";
            System.out.println(res.substring(0, res.length() - 1));
        }
    }
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值