HDU2001java

import java.util.*;
import java.text.DecimalFormat;
class Main{
public static void main(String args[])
{Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{ double x1=cin.nextDouble();
double y1=cin.nextDouble();
double x2=cin.nextDouble();
double y2=cin.nextDouble();

double c=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
DecimalFormat df=new DecimalFormat("#0.00");
System.out.println(df.format(c));
}
}
}

此处注意要用到DecimalFormat来确定浮点后面的想要保留的小数;

import java.util.*;
class Main{
public static void main(String args[])
{Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{double x1=cin.nextDouble();
double y1=cin.nextDouble();
double x2=cin.nextDouble();
double y2=cin.nextDouble();
aa(x2-x1,y2-y1);
}}
public static void aa(double a,double b)
{ double c=Math.sqrt(a*a+b*b);
System.out.printf("%.2f",c);
System.out.println();
}
}

也可以直接用printf直接保留两位小数;

posted @ 2016-03-17 16:55  折腾青春  阅读(203)  评论(0编辑  收藏  举报