紫爖菜 紫爖菜
紫爖菜:乃菜也,是食菜,也是菜鸟。
关注数: 4 粉丝数: 24 发帖数: 2,151 关注贴吧数: 29
有看到有人问了4个问题,然后删帖了,我把自己写的答案帖出来吧 //--1--保留20位小数 @Test public void factorialTest(){ BigDecimal aa = new BigDecimal(1); BigDecimal bb = new BigDecimal(0); for (int i = 1; i <= 20; i++) { BigDecimal tempF = factorial(i, new BigDecimal(1)); BigDecimal tempA = aa.divide(tempF, 20,1); bb = bb.add(tempA); } System.out.println(bb.doubleValue()); } private BigDecimal factorial(int n,BigDecimal total){ if (n > 0){ BigDecimal tempN = new BigDecimal(n); total = total.multiply(tempN); return factorial(n-1,total); } else { return total; } } //--2-- @Test public void rectangleTest(){ Rectangle re = new Rectangle(); re.setHigh(22.5); re.setWide(33.2); System.out.println("周长:"+re.getMeasure()); System.out.println("面积:"+re.getPerimeter()); } class Rectangle{ private double high; private double wide; //面积 public double getPerimeter(){ return high*wide; } //周长 public double getMeasure(){ return (high+wide)*2; } public double getHigh() { return high; } public void setHigh(double high) { this.high = high; } public double getWide() { return wide; } public void setWide(double wide) { this.wide = wide; } } //--3-- @Test public void studenTest() { FileReader fr = null; BufferedReader br = null; List<Double> aa = new ArrayList<Double>(); try { fr = new FileReader("C:\\Users\\test\\Desktop\\data.txt"); br = new BufferedReader(fr); String s = null; while ((s = br.readLine()) != null) { String[] bb = s.trim().split(" "); for (String str : bb) { aa.add(Double.valueOf(str)); } } } catch (Exception e) { e.printStackTrace(); } finally { try { fr.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } Double total = new Double("0"); Double max = new Double("0"); for (Double dd : aa) { total = total + dd; if (dd >= max) { max = dd; } } Double p = total/aa.size(); System.out.println("平均分:"+p); System.out.println("最高分:"+max); } //--4-- @Test public void strTest(){ String aaa = "a1他2sxs是吧asdassd哈哈s2d,asd"; char[] bbb = aaa.toCharArray(); StringBuffer ccc = new StringBuffer(); for (char b : bbb) { if (String.valueOf(b).matches("[a-zA-Z\\,\\.\\?\\!]")) { ccc.append(b); } } System.out.println(ccc.toString()); }
1 下一页