用Java编写程序在控制台窗口中提示输入两个整数,然后接收这两个整数,并输出它们的

2024-11-26 10:03:53
推荐回答(2个)
回答1:

不输入整数会报错:
import java.util.Scanner;

public class PrintInt {

public static void main(String[] args) {
String a,b;
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("输入第一个整数 : ");
a = scan.next();
System.out.println("输入第二个整数 :");
b = scan.next();
try{
System.out.println(Integer.parseInt(a));
System.out.print(Integer.parseInt(b));
}catch(NumberFormatException e) {
System.out.println("请输入整数!");
}
}
}

}

回答2:

import java.util;
public class test{
int a;
int b;
Scanner cin = new Scanner();
a = cin.next;
b = cin.next;
System.out.println(a + b);
}