输入两个整数a,b,输出(a+b)。要求可以输入多组a,b的...10
发布网友
发布时间:2024-02-21 18:19
我来回答
共2个回答
热心网友
时间:2024-03-17 07:56
public class Util {
public static void main(String[] args) {
Sc s = new Sc();
s.add();
}
}
class Sc{
public String add(){
java.util.Scanner sc = new java.util.Scanner(System.in);
try {
System.out.println("请输入a的值.");
int a = sc.nextInt();
System.out.println("请输入b的值.");
int b = sc.nextInt();
System.out.println(a+" + "+b + " = " + (a+b));
System.out.println("是否继续(Y/N)");
String num = sc.next();
if("Y".equals(num)){
new Sc().add();
}else if("N".equals(num)){
System.exit(0);
}else{
System.out.println("输入有误,重新开始..");
new Sc().add();
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("只能是整数...");
}
return null;
}
}
热心网友
时间:2024-03-17 07:59
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a;
int b;
while(true){
System.out.println("请输入a");
a=sc.nextInt();
System.out.println("请输入b");
b=sc.nextInt();
System.out.println("a+b="+a+b);
}
}