public static void main(String[] args) {
10多年的撫州網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整撫州建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“撫州網(wǎng)站設(shè)計(jì)”,“撫州網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
//這種方法效率應(yīng)該不是很好,你可以從面額最大的下手。然后依次遞減
Scanner scanner=new Scanner(System.in);
Double d=scanner.nextDouble();//輸入的商品價格
Double d1=5.00;//用戶支付
Double d2=d1-d;//應(yīng)該找零
if (d2!=0) {
int x= (int) (d2/2);//兩元
d2=d2%2;
int y= (int) (d2/1);//一元
d2=d2%1;
int z=(int) (d2/0.5);//五角
d2=d2%0.5;
int w=(int)(d2/0.2);//兩角
d2=d2%0.2;
int j=(int)(d2/0.1);//一角
d2=d2%0.1;
int i=(int)(d2/0.02);//兩分
d2=d2%0.02;
int k=(int)(d2/0.01);//兩分
d2=d2%0.01;
System.out.println("兩元"+x+"張");
System.out.println("一元"+y+"張");
System.out.println("五角"+z+"張");
System.out.println("兩角"+w+"張");
System.out.println("一角"+j+"張");
System.out.println("兩分"+i+"張");
System.out.println("兩分"+k+"張");
}
}
幫你改了下,自己琢磨琢磨吧
重點(diǎn)看輸入流,這個應(yīng)該經(jīng)常用到的
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class fcy{
public static void main(String[] args){
BufferedReader br=null;
try{
br=new BufferedReader(new InputStreamReader(System.in));
int a=0;
int b=0;
System.out.print("請輸入消費(fèi)金額:");
a=Integer.parseInt(br.readLine());
System.out.print("請輸入支付金額:");
b=Integer.parseInt(br.readLine());
int c=b-a;
System.out.print("余額:"+c);
int d=5000;
int e=1000;
int f=500;
int g=100;
int h=50;
int i=10;
int y=1;
int j=c/d;
int k=(c-d*j)/e;
int m=(c-d*j-e*k)/f;
int n=(c-d*j-e*k-f*m)/g;
int o=(c-d*j-e*k-f*m-g*n)/h;
int p=(c-d*j-e*k-f*m-g*n-o*h)/i;
int z=(c-d*j-e*k-f*m-g*n-o*h-p*i)/y;
System.out.println("五千元:"+j);
System.out.println("千元:"+k);
System.out.println("五百元:"+m);
System.out.println("百元:"+n);
System.out.println("五十元:"+o);
System.out.println("十元:"+p);
System.out.println("一元:"+z);
}
catch(IOException e){
System.out.println("錯誤");
}
}
}
import?java.io.IOException;
import?java.io.InputStream;
import?java.util.ArrayList;
import?java.util.List;
public?class?Change?{
/**
?*?@param?args
?*/
public?static?void?main(String[]?args)?{
System.out.println("請輸入價格:");
double?m?=?Double.parseDouble(getInputLine());
System.out.println("請付款金額:");
double?c?=?Double.parseDouble(getInputLine());
change(m,?c);
}
public?static?String?getInputLine()?{
InputStream?inputStream?=?System.in;
ListByte?cmdBuffer?=?new?ArrayListByte();
int?t;
try?{
while?(true)?{
t?=?inputStream.read();
if?(t?==?'\n')?{
byte[]?bs?=?new?byte[cmdBuffer.size()];
for?(int?i?=?0;?i??cmdBuffer.size();?i++)?{
bs[i]?=?cmdBuffer.get(i);
}
return?new?String(bs).trim();
}?else?{
cmdBuffer.add((byte)?t);
if?(cmdBuffer.size()??1024)?{
return?null;
}
}
}
}?catch?(IOException?e)?{
return?null;
}
}
public?static?double[]?moneys?=?{?0.01,?0.02,?0.05,?0.1,?0.2,?0.5,?1,?2,?5,?10,?20,?50,?100?};
/**
?*?@param?m
?*????????????價格
?*?@param?c
?*????????????付款
?*/
public?static?void?change(double?m,?double?c)?{
double?z?=?c?-?m;
if?(z??0)?{
System.out.println("付款不足!");
}?else?if?(z?==?0)?{
System.out.println("不需要找零。");
}?else?{
System.out.println("找零共:"?+?z?+?"元");
System.out.println("其中:");
for?(int?i?=?moneys.length?-?1;?i?=?0;?i--)?{
double?money?=?moneys[i];
int?count?=?0;
while?((z?-?money)?=?0)?{
z?-=?money;
count++;
}
if?(count??0)?{
System.out.println(money?+?"元:"?+?count?+?"張");
}
}
}
}
}