
正文
java编程超市找钱代码 java超市购物程序代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA 递归找零钱
看了楼上的写法后,不甚满意,于是自己写了一种。
这个题目很有意思,相当有意思。
加上了比较详细的注释。
你试一试。
这种方法你能很明显的看出递归的特征。
可以随意传要找钱的总数,以及硬币面额可以增加或者减少。但是
硬币的面额要由小到大排列,就ok
注释加的有点多。。看起来有点累赘。不过能容易看。
package file;
import java.util.ArrayList;
import java.util.List;
/**
* 递归得到找钱方案个数
* 思路为:
* 定义待找钱币类型。默认从小到大排列。
* 传入参数后,先获得最大的硬币面值。通过整除,得到可以有几种找钱情况
* 递归调用找钱,因为排除了最大面额硬币后的可找硬币以余额都发生了变化。可以看作一次新的找钱
* 最终得到所有的找钱方案。封装到List中返回。
* @version TestRePay.java v. 1.0.0 2010-10-26
* @author Andy
*/
public class TestRePay {
public static void main(String[]args){
int i = 150;
int[] coinType = new int[]{1,5,10,25,50};
//调用递归方法
ListString allResult = reCoins(coinType, i);
System.out.println("找钱方案的个数为:"+allResult.size());
for(int j = 1 ,k = allResult.size(); j=k;j++ ){
System.out.println(" 第"+j+"种找钱方案为:"+allResult.get(j-1));
}
}
/**
* 通过递归,得到所有的方案数量
* author Andy
* date 2010-10-26 下午01:30:06
* @param coinType 可以找给客户的钱币的类型 为int数组
* @param totalMoney 待找钱的数量
* @return
*/
public static ListString reCoins(int[]coinType , int totalMoney){
//获得最后一个,也就是默认为最大的一个钱币类型
int lastCoin = coinType[coinType.length-1];
//通过数组复制获得下一级调用时候的硬币类型
int[] newCoinType = new int[coinType.length-1];
System.arraycopy(coinType, 0, newCoinType, 0, newCoinType.length);
//获得针对当前硬币,也就是这个lastCoin 有几种找钱情况
int times = totalMoney/lastCoin;
ListString resultList = new ArrayListString();
//如果当前硬币的面值大于余额。但是手中还有更小面值的硬币,则递归调用找钱。
if(times==0coinType.length!=0){
ListString childList = reCoins(newCoinType, totalMoney);
resultList.addAll(childList);
//进行遍历,得到带找钱的值
}else{
//最小面值了,所以,直接返回值
if(coinType.length==1){
resultList.add(" 1分*"+totalMoney+"个 ");
}else{
//不是最小面值,所以,要遍历下
for(int i=1;i=times;i++){
ListString childList = null;
int remainder = totalMoney-lastCoin*i;//新余额
if(remainder != 0){
childList = reCoins(newCoinType, remainder);
for(String r:childList){
r = " "+lastCoin+"分*"+i+"个 " + r;
resultList.add(r);
}
}else{
resultList.add(" "+lastCoin+"分*"+i+"个 ");
}
}
}
}
return resultList;
}
}
加油,java的路 还有很长哦,呵呵
相关问答
Q1: 用java怎么编写
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String temp = "";
int num = 0;
double totalAll = 0;
while (!"y".equals(temp)) {
System.out.println("*********************************");
System.out.println("请选择购买的商品编号java编程超市找钱代码:");
System.out.println("1、T恤 2、网球鞋 3、网球拍");
System.out.println("*********************************");
System.out.println("请输入商品编号java编程超市找钱代码:");
temp = in.next();
System.out.println("请输入商品数量java编程超市找钱代码:");
num = in.nextInt();
if ("1".equals(temp)) {
double value = 245;
double total = num * value;
totalAll += total;
System.out.println("T恤 ¥" + value + " ¥" + total);
} else if ("2".equals(temp)) {
double value = 570;
double total = num * value;
totalAll += total;
System.out.println("网球鞋 ¥" + value + " ¥" + total);
} else if ("3".equals(temp)) {
double value = 320;
double total = num * value;
totalAll += total;
System.out.println("网球拍 ¥" + value + " ¥" + total);
}
System.out.println("是否退出 y/n");
temp = in.next();
}
if ("y".equals(temp)) {
double count = 0.8;
System.out.println("请缴费:");
double inputAmount = in.nextInt();;
System.out.println("折扣:" + count);
System.out.println("总计:" + totalAll * count);
System.out.println("缴费:" + inputAmount);
if ((inputAmount - totalAll * count) = 0) {
System.out.println("找零:" + (inputAmount - totalAll * count));
} else {
System.out.println("来人啊java编程超市找钱代码,乱棍打出去:");
}
}
in.close();
}
Q2: 数据库如何用java写超市购物程序?
要使用Java编写超市购物程序,您需要使用Java数据库编程技术来连接到您的数据库并执行相关的数据库操作。在Java中,您可以使用JDBC(Java数据库连接)API来连接到数据库并执行SQL语句。例如,您可以使用JDBC API来执行以下操作:
创建数据库连接
执行增删改查(CRUD)操作
提交或回滚事务
执行存储过程和函数
查询数据库元数据
此外,您还可以使用Java的面向对象编程技术来封装数据库操作,以便更方便地在您的程序中使用。例如,您可以创建一个类来表示超市商品,并定义一些方法来执行商品的增删改查操作。这样,您就可以在程序中通过调用这些方法来简单地完成对数据库的操作,而不需要编写复杂的SQL语句。
Q3: 求帮忙 java 编写程序,急急急!!!计算机考试
import java.math.BigDecimal;
public class Demo {
public static Double[] types = { 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0 };// 人民币币种
public static void main(String[] args) {
Double toMoney = 100.0;// 用于支付的100元
Double tpendMoney = 81.99;// 实际花费81.99元
Double over = DoubleFormat(toMoney - tpendMoney);
System.out.println("找零" + over);
String calc = calc(over);
System.out.println(calc);
}
public static String calc(Double over) {
String resultString = "";
for (int i = 0; i types.length; i++) {
if (types[i] = over) {
if (types[i].equals(over)) {
return types[i].toString();
} else {
resultString += types[i - 1];
resultString += "\t" + calc(DoubleFormat(over - types[i - 1]));
break;
}
}
}
return resultString;
}
public static Double DoubleFormat(Double d) {
BigDecimal bd = new BigDecimal(d);
return bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
结果:
找零18.01
面值:10.0 , 5.0 , 2.0 , 1.0, 0.01
Q4: 怎么用JAVA编译升级购物结算? 有商品名称、商品金额、继续购买的循环、折扣、总计金额、交费金额、找钱
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import org.apache.commons.lang.StringUtils;
public class Test
{
public static void main(String []args){
System.out.println("******************************************************************************************************");
System.out.println("请选择要购买的商品编号:");
System.out.println("1.T恤(245) 2.网球鞋(570) 3.网球拍(320)");
System.out.println("******************************************************************************************************");
Long total = 0L;
//应付费用和找零保留两位小数点
DecimalFormat df = new DecimalFormat("#.00");
BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
//接受用户输入的变量,商品编码、商品数量、是否继续和实际交费
String code = "";String num = "";String isBuy = "";String pay = "";
Double discount = 0.8;
int number = 0;Long payMoney = 0L;Boolean flag = false;
try
{
//do...while循环,先执行do,然后在用户输入y的情况下循环执行do中的语句
do{
code ="";num="";isBuy="";
//如果输入编号有误则循环重新输入
do{
flag=false;
System.out.print("请输入商品编号:"+code);
//获取用户输入的编码
code = strin.readLine();
if(code.equals("1")||code.equals("2")||code.equals("3")){
flag = true;
}else{
flag=false;
System.out.print("商品编号不存在!");
}
}while(flag == false);
//输入不符合要求时循环重新输入
do{
flag = false;
System.out.print("请输入商品数量:"+num);
//获取用户输入的数量
num = strin.readLine();
//在输入数量不为空的情况下,将数量转换成整型,若转换失败说明输入的不是整型数据,提示用户重新输入
if(StringUtils.isNotEmpty(num)){
try
{
number = Integer.parseInt(num);
flag = true;
}
catch (Exception e)
{
System.out.println("商品数量请输入正整数!");
flag = false;
num = "";
}
}
}while(flag == false);
//根据用户输入情况计算当前费用
if(code.equals("1")){
total += 245*number;
System.out.println("T恤 ¥"+245.0 +" ¥"+total);
System.out.print("是否继续(y/n)"+isBuy);
isBuy = strin.readLine();
}else if(code.equals("2")){
total+=570*number;
System.out.println("网球鞋 ¥"+570.0 +" ¥"+total);
System.out.print("是否继续(y/n)"+isBuy);
isBuy = strin.readLine();
}else if(code.equals("3")){
total+=361*number;
System.out.println("网球拍 ¥"+320.0 +" ¥"+total);
System.out.print("是否继续(y/n)"+isBuy);
isBuy = strin.readLine();
}
}while(isBuy.equals("y"));
System.out.println("折扣:"+discount);
//根据折扣计算顾客需要交纳的费用
String needPay = df.format(total*discount);
System.out.println("金额总计: ¥"+needPay);
//输入不符合要求时循环重新输入
do{
flag =false;
System.out.print("实际交费: ¥"+pay);
//获取用户输入的实际交费
pay = strin.readLine();
//在输入交费不为空的情况下,将交费转换成长整型,若转换失败说明输入的不是长整型数据,提示用户重新输入
if(StringUtils.isNotEmpty(pay)){
try{
payMoney = Long.parseLong(pay);
flag=true;
}catch(Exception ex){
System.out.println("实际交费请输入数字!");
flag=false;
pay="";
}
}
}while(flag == false);
System.out.print("找钱: ¥"+(payMoney-Double.parseDouble(needPay)));
}catch (IOException e1){
e1.printStackTrace();
}
}
}
以上是能达到输出要求的代码,有通过测试,比较初级的,希望能帮到楼主。
java编程超市找钱代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java超市购物程序代码、java编程超市找钱代码的信息别忘了在本站进行查找喔。







