要求:前后2端的比例是中间长度的50%可以传入一个最小值,一个数组(这个数组里面的数字可以是任意个)和一个最大值,改变最小值也可以直接传入初始的百分比
import java.text.DecimalFormat;
/**
*
* @Date: 2019/7/7
* @Time: 23:14
* @Description: No Description
*/
public class Hello {
public static void sh (int min,int []a,int max) {
DecimalFormat df = new DecimalFormat("00.00");
float d ;
//循环输出百分比
for (int i = min; i < max; i++) {
//为了模拟数据,现在直接将数据写死
//判断第一个0.5
for(int j = 0;j<a.length;j++){
if (i < a[0]){
//首先看到比例
float x =(float) 100/a.length/2/100*i/a[0];
System.out.println(df.format(x*100)+"%");
break;
}
//中间所有的1
if(j>0&&i>a[j-1]&&i<a[j]){
int x = a[j]-a[j-1];
// d = (1 / (float)a.length) / 2 + (j-1)*((float)1 / a.length)+(((float)i - a[j-1]) / max);
// d = ((float)(Math.round(1 / (float)a.length) / 2*100)/100) + (float)(Math.round(j*((float)1 / a.length)*100)/100)+(float)(Math.round((((float)i - a[j-1]) / max)*100)/100);
//分别表示第一段的百分比+中途的百分比+当前块的百分比
d = (float) 100/a.length/2/100+(((float) 100/a.length)*(j-1)/100)+((float) 100/a.length/100*(i-a[j-1])/x);
System.out.println(df.format(d*100)+"%");
}
//最后那个0.5
if (i>a[a.length-1]&&j==3){
int y= max-a[a.length-1];
float x =((float) 100/a.length/2/100)+(((float) 100/a.length)*(j)/100+((float) 100/a.length/2/100*(i-a[j])/y));
System.out.println(df.format(x*100)+"%");
}
}
}
}
public static void main(String[] args) {
//我这里数组传入4个值
int []array ={700,2100,4600,7200};
Hello.sh(0,array,10000);
}
}