OJ在线编程输入输出(Java版)

练习地址:校招笔试真题_C++工程师、golang工程师_牛客网OJ在线编程常见输入输出练习,笔试真题面试真题在线oj练习,在线编程学习提升求职竞争力,真正的应届生校招实习求职神器。icon-default.png?t=M85Bhttps://www.nowcoder.com/test/27976983/summary#question

1.[编程题]A+B(1)

输出a+b的结果

 可以看到输入和输出是有两行的,有的题目中可能还有三行四行无限行,因此我们要使用while循环来读取数据。

可以看到所有输入都是数字,因此我们就可以选取 hasNextInt()。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a + b);
        }
    }
}

2.[编程题]A+B(2)

 这个方式给了一次输入一共有几个值,分行分次来进行计算

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 a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a + b);
        }
    }
}

3.[编程题]A+B(3)

输入数据有多组, 如果输入为 0 则结束输入

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            if(a == 0 && b == 0){
                return;
            }
            System.out.println(a + b);
        }
    }
}

4.[编程题]A+B(4)

输入数据包括多组。

每组数据一行,每行的第一个整数为这行整数的个数 n。

n 为 0 的时候结束输入。

接下来 n 个正整数,对这 n 个数求和。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int n = sc.nextInt();
            int sum = 0;
            if(n == 0){
                return;
            }
            for(int i = 0;i < n;i++){
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}

5.[编程题]A+B(5)

输入的第一行包括一个正整数 t 表示数据组数。(一共有几组)

接下来 t 行, 每行一组数据。

每行的第一个整数为这行整数的个数 n。

接下来 n 个正整数,对这 n 个数求和。

 

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            int sum = 0;
            int count = 0;
            while (sc.hasNextInt() && count < n) {
                count++;
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}

6.[编程题]A+B(6)

输入数据有多组, 每行表示一组输入数据。

每行的第一个整数为整数的个数n(1 <= n <= 100)。

接下来n个正整数, 即需要求和的每个正整数。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int n = sc.nextInt();
            int sum = 0;
            for(int i = 0;i < n;i++){
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}

7.[编程题]A+B(7)

每组数据输出求和的结果

 不知道一行有多少个数,因此我们直接一次读取一行,最后把这个数字分开再相加

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

8.[编程题]字符串排序(1)

对输入的字符串进行排序后输出

输入有两行,第一行n 第二行是n个字符串,字符串之间用空格隔开

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(sc.hasNextLine()){
            String s = sc.nextLine();
            String[] str = s.split(" ");
            Arrays.sort(str);
            for(int i = 0;i < str.length;i++){
                if (i != str.length - 1) {
                    System.out.print(str[i] + " ");
                } else {
                    System.out.print(str[i]);
                }
            }
        }
    }
}

9.[编程题]字符串排序(2)

多个测试用例,每个测试用例一行。

每行通过空格隔开,有n个字符,n<100

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String s = sc.nextLine();
            String[] strings = s.split(" ");
            Arrays.sort(strings);
            for(int i = 0;i < strings.length;i++){
                 if (i != strings.length - 1) {
                    System.out.print(strings[i] + " ");
                } else {
                    System.out.println(strings[i]);
                }
            }
        }
    }
}

总结:

1.如果全部是数字while 中可以使用 hasNextInt() 

2.如果全部是字符串,可以一行一行读,使用 hasNextLine() 

3.如果一行中既有字符串又有数字,则可以直接读取一行,hasNextLine() 。定义一个String 类型的数组,最后再按空格 spilt() ,或者其他什么要求进行分割就行。再把String 类型的数字转化成 int 类型 :Integer.parseInt()。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值