一、我们约定下面的数字表示如下的意义1.3表示1小时30分,2.7表示3小时10分,请写一个通用的函数计算下面几个数字的和。
1.3 、 2.7 、 3.4、 2.2 二、要求写一个多态的例子,一个接口Shape,它只有一个方法:void donPrint()。
1)请些两个类分别实现Shape接口;
2)修改并完成下面的类,根据命令行参数的不同调用Shape的结果不同
public Class ShapeTest{
Shape getShape(String type){
return null;
}
public static void main(String[] args){
}
}
1.3 、 2.7 、 3.4、 2.2 二、要求写一个多态的例子,一个接口Shape,它只有一个方法:void donPrint()。
1)请些两个类分别实现Shape接口;
2)修改并完成下面的类,根据命令行参数的不同调用Shape的结果不同
public Class ShapeTest{
Shape getShape(String type){
return null;
}
public static void main(String[] args){
}
}
第二题:
1)
class circle implements Shape{
public void donPrint(){
system.out.println("circle");
}class retangle implements Shape{
public void donPrint(){
system.out.println("retangle ");
}2)public Class ShapeTest{
Shape getShape(String type){
if(type == "circle")
return new circle();
else
if(type = "retangle")
return new retangle();
else
return null;
}
public static void main(String[] args){
(getShape(args[0]))->donPrint(();
}
}
供参考,直接写的,没测试
gl
System.out.println(sum(1.3,2.7,3.4,2.2));
}
/**
* 求和
* @param values
* @return
*/
private static String sum(Double... values){
int sum = 0;
for(double value:values){
sum += parse(value);
}
return sum/60 + "小时" + sum%60 + "分";
}
/**
* 将传入的值解析成 分钟 单位
* @param value
* @return
*/
private static int parse(Double value){
int i = value.intValue();
double d = value - i;
return (int)(i*60 + d*100);
}
分钟和double型相加?貌似没有规律。
class summ
{
public static void main(String[] args)
{
double a=1.3,b=2.7,c=3.4,d=2.2;
double aa=0,bb=0,cc=0,dd=0,ee=0,ff=0,gg=0;
aa=Math.floor(a);
bb=Math.floor(b);
cc=Math.floor(c);
dd=Math.floor(d);
a=a*10%10/10;
b=b*10%10/10;
c=c*10%10/10;
d=d*10%10/10;
ee=Math.floor((a+b+c+d)/0.6);
ff=Math.ceil((a+b+c+d)*10%6)/10;
gg=aa+bb+cc+dd+ee+ff;
System.out.print(gg);
}
}
运行结果:10.4
class sum
{
public static void main(String[] args)
{
int i=0;
double[] a={1.3,2.7,3.4,2.2};
double dd=0,ee=0,ff=0,gg=0;
for(double m:a){
dd += Math.floor(a[i]);} //求整数部分的和
a[i]=a[i]*10%10/10; //取小数部分的值
ee=Math.floor(dd/0.6); //求小数部分按0.6进位和的整数部分
ff=Math.ceil(dd*10%6)/10; //求小数部分按0.6进位和的小数部分
gg=ee+dd+ff;
System.out.print(gg);
}
}
运行结果:10.4
double[] time={1.3,2.7,3.4,2.2};
int z=0;
int x=0;
double x1=0;
for (int i = 0; i < time.length; i++) {
z+=(int)time[i];
x+=time[i]*10-(int)time[i]*10;
}
x1=x/6+(double)x%6/10;
System.out.println(z+x1);
}
第一题来个比较原始的算法.貌似题目有点问题,以加法来看不可能出现x.5以上的,小数部分只能加到5了,假如能加出10.1来是不是9.7也对,但是以上面的算法算不出9.7,题目是不是出的很无聊......还是给我们留个陷阱?
int m = (int) ((90 + 190) / (1.3 + 2.7));
System.out.println("数字1代表: " + m + "分钟"); // 总共有多少个数字
float count = (float) (1.3 + 2.7 + 3.4 + 2.2);
System.out.println("总共有: " + count + "个数字"); System.out.println("总共: " + (m * count) / 60 + " 个小时");
}
public static void main(String[]args){
int get= new Ttime().getTime(new double[]{
1.3,2.7,3.4,2.2});
System.out.println("Arrays sun get:"+get+"Minute");
System.out.println("Arrays sun get:"+get/60+"hours"+get%60+"Minte");
}
public int getTime(double[] number){
int preN=0;
int postN=0;
for(double d:number){
String str=String.valueOf(d);
//使用正则表达式
String[]sub=str.split("\\.");
preN+=Integer.parseInt(sub[0]);
postN+=Integer.parseInt(sub[1]);
}
//因为不知道是返回的是小时,还是分钟,所以最后还是把总的分钟数传回去最合适
return preN*60+postN*10;
}
}