首先,初始化一个列表来存储输入的数字:List arr = new ArrayList>();使用 Scanner 类从控制台读取输入:Scanner sc = new Scanner(System.in);接下来,读取用户输入的整数:int a = sc.nextInt();如果输入的数字小于0,需要提示用户重新输入:if (a < 0) { System.out.println("你输入的数...
JAVA计算百分比参考以下操作:int num1 = 7; int num2 = 9; // 创建一个数值格式化对象 NumberFormat numberFormat = NumberFormat.getInstance(); // 设置精确到小数点后2位 numberFormat.setMaximumFractionDigits(2); String result = numberFormat.format((float) num1 / (float) num2 ...
double x_double=x*1.0;double tempresult=x/total;//NumberFormat nf = NumberFormat.getPercentInstance(); 注释掉的也是一种方法 //nf.setMinimumFractionDigits( 2 ); 保留到小数点后几位 DecimalFormat df1 = new DecimalFormat("0.00%"); //##.00% 百分比格式,后面不...
public double fun(int a,int b){ //a表示成功次数,b表示失败次数,c表示成功百分比,d表示失败百分比 double c=(a/(a+b))*100;double d=(b/(a+b))*100;//返回成功百分比 return c;//返回失败百分比 //return d;} 得到的是一个double类型的数据,具体保留几位小数啊什么的自己处理一下...
public String getPercent(Integer num,Integer total ){ String percent ;Double p3 = 0.0;if(total == 0){ p3 = 0.0;}else{ p3 = num*1.0/total;} NumberFormat nf = NumberFormat.getPercentInstance();nf.setMinimumFractionDigits(2);//控制保留小数点后几位,2:表示保留2位小数点 ...