题目:
编写一个整钱兑零程序,该程序能够将用double类型表达的钱数兑换成相应的硬币,要求精确到分。(硬币的面值有1元、5角、1角、5分、2分、1分,如果输入:12.53,结果应该为:12(1元),1(5角),1(2分),1(1分))。
我写的代码如下,大家可以运行下,看看,有BUG,但我没找出错在哪
[code]import javax.swing.*;
public class Ex_1
{
public static void main(String args[])
{
String amountString=JOptionPane.showInputDialog(null,"请输入钱数","整钱兑零",JOptionPane.QUESTION_MESSAGE);
double amount=Double.parseDouble(amountString);
int y=(int)amount; //把amount转换为整型
double y2=(double)y; //把y转换为double型
int j5=(int) ((amount-y2)/0.5);
int j1=(int)((amount-y2-j5*0.5)/0.1);
int f5=(int)((amount-y2-j5*0.5-j1*0.1)/0.05);
int f2=(int)((amount-y2-f5*0.05-j5*0.5-j1*0.1)/0.02);
int f1=(int)((amount-y2-f5*0.05-j5*0.5-j1*0.1-f2*0.02));
System.out.println(y+"(1元)"+j5+"(5角)"+j1+"(1角)"+f5+"(5分)"+f2+"1(2分)"+f1+"(1分)");
}
}
[/code]
编写一个整钱兑零程序,该程序能够将用double类型表达的钱数兑换成相应的硬币,要求精确到分。(硬币的面值有1元、5角、1角、5分、2分、1分,如果输入:12.53,结果应该为:12(1元),1(5角),1(2分),1(1分))。
我写的代码如下,大家可以运行下,看看,有BUG,但我没找出错在哪
[code]import javax.swing.*;
public class Ex_1
{
public static void main(String args[])
{
String amountString=JOptionPane.showInputDialog(null,"请输入钱数","整钱兑零",JOptionPane.QUESTION_MESSAGE);
double amount=Double.parseDouble(amountString);
int y=(int)amount; //把amount转换为整型
double y2=(double)y; //把y转换为double型
int j5=(int) ((amount-y2)/0.5);
int j1=(int)((amount-y2-j5*0.5)/0.1);
int f5=(int)((amount-y2-j5*0.5-j1*0.1)/0.05);
int f2=(int)((amount-y2-f5*0.05-j5*0.5-j1*0.1)/0.02);
int f1=(int)((amount-y2-f5*0.05-j5*0.5-j1*0.1-f2*0.02));
System.out.println(y+"(1元)"+j5+"(5角)"+j1+"(1角)"+f5+"(5分)"+f2+"1(2分)"+f1+"(1分)");
}
}
[/code]
import javax.swing.*;
public class Ex_1
{
public static void main(String args[])
{
String amountString=JOptionPane.showInputDialog(null,"请输入钱数","整钱兑零",JOptionPane.QUESTION_MESSAGE);
double amount=Double.parseDouble(amountString);
int y=(int)amount; //把amount转换为整型
double y2=(double)y; //把y转换为double型
int j5=(int) ((amount-y2)/0.5);
int j1=(int)((amount-y2-j5*0.5)/0.1);
int f5=(int)((amount-y2-j5*0.5-j1*0.1)/0.05);
int f2=(int)((amount-y2-f5*0.05-j5*0.5-j1*0.1)/0.02);
int f1=(int)((amount-y2-f5*0.05-j5*0.5-j1*0.1-f2*0.02));
System.out.println(y+"(1元)"+j5+"(5角)"+j1+"(1角)"+f5+"(5分)"+f2+"1(2分)"+f1+"(1分)");
}
[/code]
public class Ex_1
{
public static void main(String args[])
{
String amountString=JOptionPane.showInputDialog(null,"请输入钱数","整钱兑零",JOptionPane.QUESTION_MESSAGE);
double amount=Double.parseDouble(amountString);
int y=(int)amount; //把amount转换为整型
double y2=(double)y; //把y转换为double型
int j5=(int) ((amount-y2)/0.5);
int j1=(int)((amount-y2-j5*0.5)/0.1);
int f5=(int)((amount-y2-j5*0.5-j1*0.1)/0.05);
int f2=(int)((amount-y2-f5*0.05-j5*0.5-j1*0.1)/0.02);
int f1=(int)((amount-y2-f5*0.05-j5*0.5-j1*0.1-f2*0.02));
System.out.println(y+"(1元)"+j5+"(5角)"+j1+"(1角)"+f5+"(5分)"+f2+"1(2分)"+f1+"(1分)");
}
import java.util.Collection;import javax.swing.JOptionPane;public class Ex_11{
// final static Collection <Money>MoneyArray = new ArrayList<Money>(); final static Integer[] MONEY = { 10000, 5000, 1000, 500, 100, 50, 10, 5, 1 };
final static String[] WORTH = { "一百元", "五十元", "十元", "五元", "一元", "五角", "一角",
"五分", "一分" };
final static Integer ZERO = 0;
static Integer i = 0;
static Integer amount;
static Integer[] C; public static void main(String args[]) {
String amountString = JOptionPane.showInputDialog(null, "请输入钱数",
"整钱兑零", JOptionPane.QUESTION_MESSAGE);
double a = Double.parseDouble(amountString);
a = a * 100;
amount = (int) a; C = new Integer[MONEY.length]; while (Cal(MONEY[i])) {
}
} public static Boolean Cal(Integer m) {
if (amount <= ZERO) {
int c = 0;
for (Integer in : C) {
System.out.println(C[c] + "个" + WORTH[c]);
++c; }
System.out.println();
return false;
} else if (amount >= m) {
amount = amount - m;
if (C[i] != null)
C[i] = C[i] + 1;
else
C[i] = 1;
// System.out.println(c + "个" + WORTH[i]); return true;
} else {
i = i + 1; // c = 0;
return true; } }
} 这段代码为什么编译没问题,运行时报错?
错误如下:
Exception in thread "main" java.lang.NoClassDefFoundError: Ex_11 (wrong name: t
st/Ex_11)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Ex_11. Program will exit.
public class test4 {
public static void main(String args[]) {
String amountString=JOptionPane.showInputDialog(null,"saisir","re",JOptionPane.QUESTION_MESSAGE);
double amount=Double.parseDouble(amountString);
int a=(int) (amount*100);
int y=a/100;
a=a-(y*100);
int j5=a/50;
a=a-(j5*50);
int j1=a/10;
a=a-(j1*10);
int f5=a/5;
a=a-(f5*5);
int f2=a/2;
int f1=a%2;
System.out.println(y+"(1y)"+j5+"(5j)"+j1+"(1j)"+f5+"(5f)"+f2+"(2f)"+f1+"(1f)");
}
}
import javax.swing.*;
public class Ex_1
{
public static void main(String args[])
{
String amountString=JOptionPane.showInputDialog(null,"请输入钱数","整钱兑零",JOptionPane.QUESTION_MESSAGE);
double amount=Double.parseDouble(amountString);
int y=(int)amount; //把amount转换为整型
double y2=(double)y;//把y转换为double型
BigDecimal b=new BigDecimal(new Double(amount).toString()).subtract(new BigDecimal(new Double(y2).toString()));
int j5=(int) (b.doubleValue()/0.5);
int j1=(int)((b.subtract(new BigDecimal(j5*0.5+"")).doubleValue())/0.1);
BigDecimal b1=(b.subtract(new BigDecimal(j5*0.5+"")).subtract(new BigDecimal(j1*0.1+"")));
int f5=(int)(((b1.doubleValue()))/0.05);
BigDecimal b2=b1.subtract(new BigDecimal(f5*0.05+""));
int f2=(int)((b2.doubleValue())/0.02);
BigDecimal b3=b2.subtract(new BigDecimal(f2*0.02+""));
int f1=(int)((b3.doubleValue()));
System.out.println(y+"(1元)"+j5+"(5角)"+j1+"(1角)"+f5+"(5分)"+f2+"(2分)"+f1+"(1分)");
}
}
输入12.67
测试结果:12(1元)1(5角)1(1角)1(5分)1(2分)0(1分)
可以看一下《java Puzzlers》第2还是3(记不清了)谜题,很基础的问题,结局方法像我11楼那样