重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.io.IOException;
创新互联是专业的呼和浩特网站建设公司,呼和浩特接单;提供网站建设、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行呼和浩特网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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 + "张");
}
}
}
}
}
public static void main(String[] args) {
//这种方法效率应该不是很好,你可以从面额最大的下手。然后依次递减
Scanner scanner=new Scanner(System.in);
Double d=scanner.nextDouble();//输入的商品价格
Double d1=5.00;//用户支付
Double d2=d1-d;//应该找零
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+"张");
}
}
private static final int[] m = {100,50,20,10,5,2,1};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int f = scanner.nextInt();
int[] amount = new int[f];
for(int i = 0 ; i f;i++){
amount[i] = giveChange(scanner.nextInt());
}
for(int i = 0 ; i amount.length;i++){
System.out.println(amount[i]);
}
}
public static int giveChange(int n) {
int num=0;
for(int i=0;im.length;i++){
num+=n/m[i];
n=n%m[i];
}
return num;
}
public class Change {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("The goods cost:");
int cost=sc.nextInt();
System.out.print("The customer paid:");
int paid=sc.nextInt();
change(cost,paid);
}
public static void change(int cost,int paid){
//unit of costpaid is dollar
int change=(paid-cost)*100;
int numOfTF=0;//number of 25 cents
int numOfT=0;//number of 10 cents
int numOfF=0;//number of 5 cents
if(change0)//payment is not enough
System.out.println("payment is not enough");
else{
numOfTF=(int)change/25;
numOfT=(int)(change-numOfTF*25)/10;
numOfF=(int)(change-numOfTF*25-numOfT*10)/5;
System.out.println("Change is: "+numOfTF+" 25 cents, "+numOfT+" 10 cents, "+numOfF+" 5 cents.");
}
}
}
下附代码,可以在键盘输入加入判断,比如是否输入的是数字而不是字母等等,是否输入的是正数而不是其他符号... 另外可以将下面代码优化,封装,在main方法只做调用即可
package test;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
boolean flag = true;
while(flag){
System.out.println("欢迎使用自动饮料售卖机");
System.out.println("1.雪碧 3.00");
System.out.println("2.可乐 3.00");
System.out.println("3.特仑苏 5.00");
System.out.println("4.农夫山泉 2.00");
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("请输入你的选择是:");
int choise = input.nextInt();
System.out.print("请投币:");
int money = input.nextInt();
money(money, choise);
System.out.print("是否继续'1.继续 0.退出' :");
int goon = input.nextInt();
if(goon==1){
flag = true;
}else{
flag = false;
}
}
}
public static void money(int money,int choise){
switch(choise){
case 1:
if(money-30){
System.out.println("找零"+(money-3)+"元");
}else if(money-30){
System.out.println("请再投入"+(3-money)+"元");
}else if(money-3==0){
System.out.println("生活愉快");
}else{
System.out.println("输入有误!");
}
break ;
case 2:
if(money-30){
System.out.println("找零"+(money-3)+"元");
}else if(money-30){
System.out.println("请再投入"+(3-money)+"元");
}else if(money-3==0){
System.out.println("生活愉快");
}else{
System.out.println("输入有误!");
}
break ;
case 3:
if(money-50){
System.out.println("找零"+(money-5)+"元");
}else if(money-50){
System.out.println("请再投入"+(5-money)+"元");
}else if(money-5==0){
System.out.println("生活愉快");
}else{
System.out.println("输入有误!");
}
break ;
case 4:
if(money-20){
System.out.println("找零"+(money-2)+"元");
}else if(money-20){
System.out.println("请再投入"+(2-money)+"元");
}else if(money-2==0){
System.out.println("生活愉快");
}else{
System.out.println("输入有误!");
}
break ;
}
}
}
帮你改了下,自己琢磨琢磨吧
重点看输入流,这个应该经常用到的
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("请输入消费金额:");
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("错误");
}
}
}