输入练习
编写一个程序从标准输入终端上两个大于0的整数a和b,计算a处以b得到c,并将结果保留两位小数打印在标准输出设备上。
如果输入的内容不符合要求,则提示“Error Input”,程序退出。如下所示:
若b=0则如夏所示
代码如下:
import java.util.*;
public class D0006 {
public static void main(String[] agrs){
Scanner input=new Scanner(System.in);
System.out.println("input a:");
String a=input.nextLine();
if(a!=null&&a.matches("^[0-9]+$"));
else{
System.out.print("Error Input");
return;
}
System.out.println("input b:");
String b=input.nextLine();
if(b!=null&&b.matches("^[0-9]+$"));
else{
System.out.print("Error Input");
return;
}
Integer integer;
Integer integer1;
integer = Integer.valueOf(a);
integer1 = Integer.valueOf(b);
if(integer1.intValue()==0){
System.out.print("Inf");
return;
}
double c=+integer.intValue()/integer1.intValue();
System.out.printf("c=%.2f",c);
}
}
(全文完)