Java练习-购物小票

这篇博客详细介绍了如何使用Java编程实现购物小票的生成过程,包括商品信息的处理、总价计算以及小票格式的设定。通过实例代码解析,帮助读者理解Java在实际应用场景中的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/*
 * 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");



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值