开始
牛客变成一定要先导包,然后写一个Main类,然后写main方法,注意main方法的参数是String[] args
import java.util.*;
public class Main{
public static void main(String[] args){
}
}
1.计算a+b(1)
注意:当没说输出多少组数据的时候,也就是不知道循环次数的时候,就可以用sc.hasNext()
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();
System.out.println(a+b);
}
}
}
2.计算a+b(2)
已经给了循环次数t,这时候就可以直接确定循环次数了,用for或者while循环
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a+b);
}
}
}
3.计算a+b(3)
如果碰到 0 0 就提前结束 break
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) break;
System.out.println(a+b);
}
}
}
4.计算a+b(4)
import java.util.*;
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) break;
int sum = 0;
while(n-- > 0){
sum += sc.nextInt();
}
System.out.println(sum);
}
}
}