题目1
import java.util.Scanner;
/**
* 输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()){
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a + b);
}
}
}
同时while循环也是为了保证能够一直在控制台输入数据,才这么做的
题目2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String s = scanner.nextLine();
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a + b);
}
}
}
同时while循环也是为了保证能够一直在控制台输入数据,才这么做的
题目3
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();
int b = sc.nextInt();
if(a != 0 && b != 0) {
System.out.println(a + b);
}else {
return;
}
}
}
}
题目4
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();
int sum = 0;
if(a == 0) {
continue;
}
for(int i = 0 ; i < a ;i++) {
sum += sc.nextInt();
}
System.out.println(sum);
}
}
}
题目5
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int count = sc.nextInt();
for(int i = 0 ; i < count ; i++) {
int a = sc.nextInt();
int sum = 0;
if(a == 0) {
continue;
}
for(int j = 0 ; j < a ;j++) {
sum += sc.nextInt();
}
System.out.println(sum);
}
}
}
}
题目6
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();
int sum = 0;
if(a == 0) {
continue;
}
for(int j = 0 ; j < a ;j++) {
sum += sc.nextInt();
}
System.out.println(sum);
}
}
}
题目7
像没说明有几个测试用例的一般都要用while循环
import java.util.*;
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();
String[] str1 = str.split(" ");
for(int i = 0 ; i < str1.length ; i++) {
sum += Integer.parseInt(str1[i]);
}
System.out.println(sum);
}
}
}
题目8
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int sum = Integer.parseInt(in.nextLine());
String[] arr = in.nextLine().split(" ");
Arrays.sort(arr);
for (String e : arr)
System.out.print(e + " ");
}
}
题目9
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) {
String[] arr = in.nextLine().split(" ");
Arrays.sort(arr);
System.out.println(String.join(" ", arr));
}
}
}
题目10
方法1
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) {
String[] arr = in.nextLine().split(",");
StringBuffer sb = new StringBuffer();
Arrays.sort(arr);
for(int i = 0 ; i < arr.length ; i++) {
if(i != arr.length - 1) {
sb.append(arr[i]);
sb.append(",");
}else {
sb.append(arr[i]);
}
}
System.out.println(sb.toString());
}
}
}
方法2
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) {
String[] arr = in.nextLine().split(",");
Arrays.sort(arr);
System.out.println(String.join(",", arr).trim());
}
}
}
题目11
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextLong()){
long a = in.nextLong();
long b = in.nextLong();
System.out.println(a+b);
}
}
}