计算钱币

编写程序,读取用户输入的代表总金额的double值,打印表示该金额所需的最少纸币张数和硬币个数,打印从最大金额开始。纸币的种类有十元、五元、一元,硬币的种类有五角、一角、贰分、壹分。

比如说需要找给客人47.63元,利用程序得出应该给客人4 张十元,1 张五元,2 张一元,1 个五角,1 个一角,1 个贰分,1 个壹分

代码如下:
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
float input = scanner.nextFloat(); //几元
int num = (int) (input * 100); // 几分 //强制转换 //这里乘了100,成了整数
int a = num / 1000; // 几张十元
num = num - a * 1000; //相当于这里把所有的十元给客人了
int b = num /500; // 几张五元
num = num - b * 500; //相当于把十元五元都给了,准备给更小单位的钱
int c = num / 100; // 几张一元
num = num - c * 100;
int d = num / 50; // 几个五角
num = num - d * 50;
int e = num / 10; // 几个一角
num = num - e *10;
int f = num / 2; // 几个两分
num = num - f * 2; // 几个一分
System.out.println(a + “张十元”);
System.out.println(b + “张五元”);
System.out.println(c + “张一元”);
System.out.println(d + “个五角”);
System.out.println(e + “个一角”);
System.out.println(f + “个两分”);
System.out.println(num + “个一分”);
}
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Private Function setdata(num As Integer) As String '数字转换 Select Case num Case 0 setdata = "零" Case 1 setdata = "壹" Case 2 setdata = "贰" Case 3 setdata = "叁" Case 4 setdata = "肆" Case 5 setdata = "伍" Case 6 setdata = "陆" Case 7 setdata = "柒" Case 8 setdata = "捌" Case 9 setdata = "玖" End Select End Function Private Function chang(aaa As Integer) As String '位数转换 Select Case aaa Case 1 chang = "" Case 2 chang = "十" Case 3 chang = "百" Case 4 chang = "千" Case 5 chang = "万" Case 6 chang = "十" Case 7 chang = "百" Case 8 chang = "千" Case 9 chang = "亿" Case 10 chang = "十" End Select End Function Private Sub Form_Activate() '设定文本长度 Text2.MaxLength = 10 Text2.SetFocus End Sub Private Sub Text2_Change() '小写转大写 Dim i As Integer Dim j As Integer Dim myint As Integer Dim myint1 As Integer Dim mydoub As Double Dim mystr As String Dim mystr1 As String Dim mystr2 As String Dim mystr3 As String Dim mystr4 As String Dim money As Long Dim money1 As Integer Dim money2 As Long mystr = Text2.Text myint = InStr(mystr, ".") If myint = 0 Then mystr = Text2.Text Else mystr3 = Right(Text2.Text, Len(Text2.Text) - myint) If mystr3 <> "" Then '转换小数位 mystr4 = Left(mystr3, 1) mystr3 = Right(mystr3, Len(mystr3) - 1) If mystr4 <> "0" Then mystr2 = mystr2 + setdata(Val(mystr4)) + "角" End If If mystr3 <> "" Then mystr4 = Left(mystr3, 1) mystr2 = mystr2 + setdata(Val(mystr4)) + "分" End If End If mystr = Left(Text2.Text, myint - 1) End If j = Len(mystr) For i = 1 To Len(mystr) '转换整数位 money2 = Left(mystr, i) money1 = Right(money2, 1) If money1 = 0 Then If j = 5 Then If Right(mystr1, 1) <> "万" Then mystr1 = mystr1 & "万" Else If Right(mystr1, 1) <> "零" And Right(money, j) > 0 Then mystr1 = mystr1 & "零" End If Else mystr1 = mystr1 & setdata(money1) + chang(j) End If j = j - 1 Next i Text1.Text = mystr1 & "元" & mystr2 '显示大写 End Sub Private Sub Command1_Click() End End Sub

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值