T001_UT001_0006

编写一个程序从标准输入终端上两个大于0的整数a和b,计算a处以b得到c,并将结果保留两位小数打印在标准输出设备上。

1
2
3
4
5
input a:
10
input b:
1
c=10.00
如果输入的内容不符合要求,则提示“Error Input”,程序退出。如下所示:

1
2
3
input a:
abc
Error Input
1
2
3
4
5
input a:
10
input b:
abc
Error Input
若b=0则如夏所示

1
2
3
4
5
input a:
10
input b:
0
Inf

注意:

  1. 最后没有换行。
  2. "input a:" 和 “input b:”后各有一个换行
  3. 输入内容后存在一个换行

import java.util.Scanner;
import java.text.DecimalFormat;
public class T001_UT001_0006
{
	public static void main(String[] agrs)
	{
		Scanner njp_input=new Scanner(System.in);
		double njp_a,njp_b,njp_c;
		try
		{
			System.out.println("input a:");
			njp_a=njp_input.nextInt();
			System.out.println("input b:");
			njp_b=njp_input.nextInt();
			if(njp_b==0)
				System.out.print("Inf");
			else
			{	
				njp_c=(float)njp_a/njp_b;
				DecimalFormat njp_df=new DecimalFormat("0.00");
				String njp_s=njp_df.format(njp_c);
				System.out.print("c="+njp_s);
			}
		}
		catch(Exception e){System.out.print("Error Input");}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gabanon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值