Java练习-购物小票

来源:互联网 发布:windows repair使用 编辑:程序博客网 时间:2024/04/30 03:16


/* * To change this template, choose Tools | Templates * and open the template in the editor. */package schoolReport;import java.util.*;/** * * @author Administrator */public class ComputeScore {    double compute(String chengjiForm){        Scanner scanner = new Scanner(chengjiForm);//创建scanner,将chengjiForm传递给构造方法的参数        String regex = "[^0123456789.]+";        scanner.useDelimiter(regex);  //scanner调用useDelimiter(String regex),将regex传递给该方法的参数        double sum=0;        while(scanner.hasNext()){            try{                double price = scanner.nextDouble();//scanner调用nextDouble()返回数字单词                sum = sum+price;            }            catch(InputMismatchException exp){                String t=scanner.next();            }        }        return sum;            }}


/* * To change this template, choose Tools | Templates * and open the template in the editor. */package shoppingReceipt;import checkIntroduction.*;/** * * @author Administrator */public class ShowComputePice {    public void showComputePice(){        String s1="苹果:56.7元,香蕉:12元,芒果:19.8元";        String s2="酱油:6.7元,精盐:0.8元,榨菜:9.8元";        ComputePice jisuan = new ComputePice();        String regex = "[^0123456789.]+";//匹配所有非数字字符串        String s1_number = s1.replaceAll(regex, "*");       //           String regex2 = "[0123456789元.]+";        String temp = s2.replaceAll(regex2, "");        temp = temp.replaceAll(":"," ");        temp = temp.replaceAll(",", " ");        System.out.println("***************");        System.out.println(temp);        System.out.println();        //        double priceSum = jisuan.compute(s1_number,"*");        System.out.printf("\"%s\"价格总和:\n%f元\n",s1,priceSum);        String s2_number = s2.replaceAll(regex, "#");        priceSum = jisuan.compute(s2_number, "#");        System.out.printf("\"%s\"价格总和:\n%f元\n",s2,priceSum);    }}


        ShowComputePice scp=new ShowComputePice();        scp.showComputePice();        System.out.printf("\n\n");



0 0