OJ online programming common input and output practice field
Java processing input and output
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
When only entering once
a、b
at a time
Example:
5 6
11
code show as below:
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)
The input includes two positive integers
a,b(1 <= a, b <= 10^9)
, and the input data includes multiple groups.
Example:
1 5
6
10 20
30
2 3
5
code show as below:
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)
The first line of input includes a data group number t(1 <= t <= 100)
Each next line includes two positive integers a, b (1 <= a, b <= 10^9)
Example:
99
1 5
6
9 8
17
6 3
9
code show as below:
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)
The input includes two positive integers
a,b(1 <= a, b <= 10^9)
, there are multiple groups of input data, if the input is, the input0 0
is ended
Example:
1 5
6
10 20
30
0 0
code show as below:
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)
The input data includes multiple groups.
Each group of data has one row, and the first integer in each row is the number of integers
n(1 <= n <= 100)
, whichn
is0
the end of the input.The next
n
positive integer is each positive integer that needs to be summed.
Example:
4 1 2 3 4
10
5 1 2 3 4 5
15
0
code show as below:
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)
The first line of input includes a positive integer
t(1 <= t <= 100)
, which indicates the number of data groups.Next
t
row, one set of data per row.The first integer in each line is the number of integers
n(1 <= n <= 100)
.The next
n
positive integer is each positive integer that needs to be summed.
Example:
2
4 1 2 3 4
10
5 1 2 3 4 5
15
code show as below:
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)
There are multiple groups of input data, and each row represents a group of input data.
The first integer in each line is the number of integersn(1 <= n <= 100)
.
The nextn
positive integer is each positive integer that needs to be summed.
Example:
4 1 2 3 4
10
5 1 2 3 4 5
15
code show as below:
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);
}
}
}