OJ在线编程常见输入输出练习场-Java版本

OJ在线编程常见输入输出练习场

Java 处理输入输出

import java.util.Scanner;

/**
 * @Author Hory
 * @Date 2020/10/19
 */
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);

        System.out.println("input name:");
        String userName = input.next();

        System.out.println("input age:");
        int userAge = input.nextInt();

        System.out.println("name is " + userName);
        System.out.println("age is " + userAge);
    }
}

A+B

每次只输入一次a、b

示例:

5 6
11

代码如下:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args){
    
    
        Scanner input = new Scanner(System.in);
      
        Integer a = input.nextInt();
        Integer b = input.nextInt();
        System.out.println(a+b);
    }
}

A+B(1)

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。

示例:

1 5
6

10 20
30

2 3
5

代码如下:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args){
    
    
        Scanner input = new Scanner(System.in);
        
        // 使用while循环判断是否有下一个输入
        while(input.hasNext()){
    
    
            Integer a = input.nextInt();
            Integer b = input.nextInt();
            System.out.println(a+b);
        }
    }
}

A+B(2)

输入第一行包括一个数据组数t(1 <= t <= 100)

接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)

示例:

99

1 5
6

9 8
17

6 3
9

代码如下:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args){
    
    
        Scanner input = new Scanner(System.in);
        
        Integer first = input.nextInt();
        
        while(input.hasNext()){
    
    
            Integer a = input.nextInt();
            Integer b = input.nextInt();
            System.out.println(a+b);
        }
    }
}

A+B(3)

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组,如果输入为0 0则结束输入

示例:

1 5
6

10 20
30

0 0

代码如下:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args){
    
    
        Scanner input = new Scanner(System.in);
        
        while(input.hasNext()){
    
    
            Integer a = input.nextInt();
            Integer b = input.nextInt();
            if(a == 0 && b == 0){
    
    
                return;
            }
            System.out.println(a+b);
        }
    }
}

A+B(4)

输入数据包括多组。

每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100)n0的时候结束输入。

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

示例:

4 1 2 3 4
10

5 1 2 3 4 5
15

0

代码如下:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args){
    
    
        Scanner input = new Scanner(System.in);
        
        while(input.hasNext()){
    
    
            Integer num = input.nextInt();
            if(num <= 0) return;
            Integer sum = 0; // 总和
            int i = 0; // 记录输入的个数
            while(input.hasNext() && i < num){
    
    
                Integer newInput = input.nextInt();
                i++;
                sum += newInput;
            }
            
            System.out.println(sum);
        }
    }
}

A+B(5)

输入的第一行包括一个正整数t(1 <= t <= 100),表示数据组数。

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

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

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

示例:

2

4 1 2 3 4
10

5 1 2 3 4 5
15

代码如下:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args){
    
    
        Scanner input = new Scanner(System.in);
        
        Integer arrNum = input.nextInt(); // 组数
        if(arrNum <= 0) return;
        
        while(input.hasNext()){
    
    
            Integer num = input.nextInt();
            if(num <= 0) return;
            Integer sum = 0; // 总和
            int i = 0; // 记录输入的个数
            while(input.hasNext() && i < num){
    
    
                Integer newInput = input.nextInt();
                i++;
                sum += newInput;
            }
            System.out.println(sum);
        }
    }
}

A+B(6)

输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)
接下来n个正整数, 即需要求和的每个正整数。

示例:

4 1 2 3 4
10

5 1 2 3 4 5
15

代码如下:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args){
    
    
        Scanner input = new Scanner(System.in);
        
        while(input.hasNext()){
    
    
            Integer num = input.nextInt();
            if(num <= 0) return;
            Integer sum = 0; // 总和
            int i = 0; // 记录输入的个数
            while(input.hasNext() && i < num){
    
    
                Integer newInput = input.nextInt();
                i++;
                sum += newInput;
            }
            
            System.out.println(sum);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44471490/article/details/109168848
今日推荐